• js验证input输入正整数 和 输入的金额小数点后保留两位(PC端键盘输入)


    // 验证开头不为零的正整数
    WST.zhengZhengShuIn = function (className){
        var rex = /^[1-9]{1}[0-9]*$/;//正整数
        $("."+className).keyup(function(event){
            var event = event || window.event;
            var value = $(this).val();
            if(value.length>1){
                var flagRex =/^0/;
                if(flagRex.test(value)){
                    layer.msg('首个字符不能为0',{time:1500});
                    $(this).val('');
                }
            }
        });
        $("."+className).keypress(function(event){
            var event = event || window.event;
            if (event.charCode == 46 && event.charCode !== 0 && event.charCode < 48 || event.charCode > 57) {
                return false;
            }
            if (/./.test($(this).val()) && event.charCode === 46) {
                return false;
            }
            if ($(this).val() === "" && event.charCode === 46) {
                return false;
            }
            var tails = ($(this).val()).split(".")[1];
            if (tails && tails.length === 2 && event.charCode !== 0) {
                return false;
            }
        });
    
    }
    //验证 价格小数点后保留两位
     WST.onlyPriceNum = function (obj) {
        var priceRegex = /^d+.?d{0,2}$/;
        obj.blur(function(event) {
            var event = event || window.event;
            var val = $(this).val();
            var newVal = val.replace(/[^d.]/g, "");
            newVal = newVal.replace(/^./g, "");
            newVal = newVal.replace(/.{2,}/g, ".");
            newVal = newVal.replace(".", "$#$").replace(/./g, "").replace("$#$", ".");
            newVal = newVal.replace(/^(-)*(d+).(dd).*$/, '$1$2.$3');
            $(this).val(newVal);
        });
        obj.keyup(function(event) {
            var event = event || window.event;
            var val = $(this).val();
            var newVal = val.replace(/[^d.]/g, "");
            newVal = newVal.replace(/^./g, "");
            newVal = newVal.replace(/.{2,}/g, ".");
            newVal = newVal.replace(".", "$#$").replace(/./g, "").replace("$#$", ".");
            if (newVal.length > 0) {
                var valArray = newVal.split(".");
                var index = newVal.indexOf(".");
                if (valArray.length === 1) {
                    if (index !== -1) {
                        newVal = parseInt(valArray[0]) + ".";
                    } else {
                        newVal = parseInt(valArray[0]) + "";
                    }
                } else {
                    newVal = parseInt(valArray[0]) + "." + valArray[1];
                }
            }
            newVal = newVal.replace(/^(-)*(d+).(dd).*$/, '$1$2.$3');
            $(this).val(newVal);
        });
        obj.keypress(function(event) {
            if (event.charCode !== 46 && event.charCode !== 0 && event.charCode < 48 || event.charCode > 57) {
                return false;
            }
            if (/./.test($(this).val()) && event.charCode === 46) {
                return false;
            }
            if ($(this).val() === "" && event.charCode === 46) {
                return false;
            }
            var tails = ($(this).val()).split(".")[1];
            if (tails && tails.length === 2 && event.charCode !== 0) {
                return false;
            }
        });
    }
  • 相关阅读:
    爬虫笔记:PyQuery模块(七)
    25丨固若金汤的根本(下):数字签名与证书
    爬虫笔记:Beautiful Soup 使用(六)
    24丨固若金汤的根本(上):对称加密与非对称加密
    爬虫笔记:requests模块使用(四)
    五安全篇(7讲)23-TLS又是什么?
    爬虫笔记:http请求详解(三)
    22丨冷链周转:HTTP的缓存代理
    python中expandtabs()函数的用法
    Python全栈工程师(while、占位符)
  • 原文地址:https://www.cnblogs.com/lst619247/p/8618440.html
Copyright © 2020-2023  润新知