// 验证开头不为零的正整数
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+)\.(\d\d).*$/, '$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+)\.(\d\d).*$/, '$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;
        }
    });
}

 

posted on 2018-03-21 17:32  刘世涛6192  阅读(391)  评论(0编辑  收藏  举报