jquery validator简单使用,微复杂度密码校验,错误信息集中展示

通用设置

var CC={};
CC.validate = {
    // focusCleanup: false,
    //    focusInvalid: false,
    onclick: function() {
        return false;
    },
    onfocusin: function() {
        return false;
    },
    onfocusout: function() {
        return false;
    },
    onkeyup: function() {
        return false;
    },
}
//添加校验规则
$.validator.addMethod("mobile", function(value, element, params) {
    return /^1[34578]\d{9}$/.test(value);
});
//添加校验规则,有且仅有数字、字母
$.validator.addMethod("password", function(password, element, params) {
    var iNow = 0;
    if (password.match(/\s/)) {
        return false;
    }
    if (password.match(/[\u4E00-\u9FA5]/g)) {
        return false;
    }
    if(password.match(/[0-9]/g)){
        iNow++;
    }
    if(password.match(/[a-z]/ig)){
        iNow++;
    }
    if(password.match(/[~!@#$%^&*]/g)){
        iNow++;
    }
    console.log(inow);
    if(iNow < 2){
        return false;
    }
    return true;
});

使用

function showErrorMsg(msg) {
    $('#errorDispaly').show().find('p').html(msg);
}
function clearErrorMsg() {
    $('#errorDispaly').hide().find('p').html(null);
}
$("#registerForm").validate($.extend(true, CC.validate, {
        rules: {
            mobilePhone: {//input的id
                required: true,//自带校验规则
                mobile: true//使用校验规则
            },
            password: {//input的id
                required: true,//自带校验规则
                rangelength:[6, 20],//自带校验规则
                password: true,//使用校验规则
            },
            smSCaptcha: {//input的id
                required: true,//自带校验规则
                minlength: 6,//自带校验规则
                maxlength: 6,//自带校验规则
            }
        },
        messages: {
            mobilePhone: {
                required: '请输入手机号码',
                mobile: '手机号输入有误'
            },
            password: {
                required: '请输入新密码',
                rangelength:'密码为6-20位,至少含数字、字母及特殊符号中的2种',
                password: '密码为6-20位,至少含数字、字母及特殊符号中的2种'
            },
            smSCaptcha: {
                required: '请输入手机验证码',
                minlength: '短信验证码输入有误',
                maxlength: '短信验证码输入有误',
            }
        },
        showErrors: function(errMap, errList) {
            //对错误信息集中处理
            if (errList && errList.length) {
                showErrorMsg(errList[0].message);
            } else {
                clearErrorMsg();
            }
        },
        submitHandler:function(form){
            if ($('#registerBtn').hasClass('btn-disabled')) return;

            $('#submitBtn').addClass('disabled');
            $.post("test.php", {
                mobilePhone: $('#mobile').val(),
                password: $('#passphrase').val(),
                mobileCaptcha: $('#mobileCaptcha').val(),
            }, function(res) {
                if (res.success) {
                    window.location.replace(document.referrer || '/');
               } else {
                   $('#registerBtn').removeClass('btn-disabled');
                   showErrorMsg('error');
               }
            });
            return;
        }
    }));
    $('#registerBtn').on('touchstart, click', function(e) {
        e.preventDefault();
        $('#registerForm').submit();
    })
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值