js校验字段的大集合

本文汇总了JavaScript中用于字段验证的各种方法和技巧,包括输入类型检查、格式验证、空值检查等,助你打造强大的前端表单验证机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//笔数验证(包括0跟正整数)
function checkParseInt(num) {
    var reg = /^([0]|[1-9][0-9]*)$/;
    if(reg.test(num)){
       return true;
    }else {
        return false;
    }
}

//金额验证(包括0跟小数点后两位)
 function checkParseFloat(num) {
     var reg = /^(([1-9]\d*)(\.\d{1,2})?)$|^(0\.0?([0-9]\d?))$|^(0)$/;
     if(reg.test(num)){
         return true;
     }else {
         return false;
     }
 }

//手机号加格式XXX XXXX XXXX
function mobileAddSpaceLogin (mobile){
    mobile = this.spaceStrChangeMobile(mobile);
    var myRe = /^[0-9]{4,11}$/g,
        isNum = myRe.test(mobile),
        newMobile = "";
    if (isNum) {
        if (mobile) for (var i = 1; i <= mobile.length; i++) newMobile += i % 4 === 0 ? " " + mobile[i - 1] : mobile[i - 1]
    } else newMobile = mobile;
    return newMobile;
}

//去手机空格
function spaceStrChangeMobile(str){
    return str += "",
        str = str.replace(/\ +/g, "")
}

//金额转换(格式:XXXX.XXXX.00)
 function formatMoney (num) {
        return (num.toFixed(2) + '').replace(/\d{1,4}(?=(\d{4})+(\.\d*)?$)/g, '$&,');
 }

  

//  验证身份证
function checkIsIdCard(idCard) {
    var idCardReg = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
    if(idCard == ""){
        console.log("身份证号为空!");
        return false;
    }else if(!idCardReg.test(idCard)) {
        console.log("身份证输入不合法!");
        return false;
    }
    return true;
}

//  验证邮箱
function checkIsEmail(email) {
    var emailReg = /^[a-z0-9A-Z]+[- | a-z0-9A-Z . _]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-z]{2,}$/;
    if(email == ""){
        console.log("邮箱为空!");
        return false;
    }else if(!emailReg.test(email)) {
        console.log("邮箱格式不合法!");
        return false;
    }
    return true;
}
//密码显示与否
function showEyes() {
    $(".eyesClose").on("touchend",function(){
        var password = $("#password");
        if(password.length>0){
            if(password.attr('type') === 'password') {
                $(this).addClass('eyes');
                password.attr('type', 'text');
            } else {
                $(this).removeClass('eyes');
                password.attr('type', 'password');
            }
        }
    });
}
//倒计时显示
function showTimes(){
    var countDown = 60;
    $('.getCodeBtn').val("重发("+countDown+")").attr("disabled",true);
    timer = setInterval(function() {
        countDown--;
        if(countDown) {
            $('.getCodeBtn').val("重发("+countDown+")").attr("disabled",true);
        }else {
            clearInterval(timer);
            countDown = parseInt($('#count-down').val());
            $('.getCodeBtn').val('重发').attr("disabled",false);
        }
    }, 1000);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值