jQuery Validate验证方法及教程

//实名认证 验证
$(function(){
    
    //中文姓名验证
    jQuery.validator.addMethod("zh_verify", function(value, element) {
    var tel = /^[\u4E00-\u9FA5\uf900-\ufa2d]{2,10}$/;
    return this.optional(element) || (tel.test(value));    
    }, "请输入2~10个字的中文!");
    
    //身份证号验证
    jQuery.validator.addMethod("idcard_verify", function(value, element) {
    var idcard = /^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/;
    return this.optional(element) || (idcard.test(value));
    }, "请输入正确格式的身份证号!");
    
    //jquery.validate插件验证
    $("#nameauth").validate({
        errorElement: ‘div‘,
        errorClass: ‘help-block‘,
        focusInvalid: false,
        ignore: "",
        rules: {
            tname: {
                required: true,
                zh_verify:true
            },
            idcard: {
                required: true,
                idcard_verify:true
            },
            con_idcard: {
                required: true,
                equalTo: ‘.idcard‘
            }
        },
        messages: {
            tname: {
                required: "真实姓名不能为空!",
                rangelength: "请输入2~18个中文!"
            },
            idcard: {
                required: "身份证不能为空!",
                rangelength: "请输入15~18个字符的身份证!"
            },
            con_idcard: {
                required: "确认证号不能为空!",
                equalTo: ‘确认证号与身份证号不一致!‘
            }
        },
        errorLabelContainer: "#resultMsg",
        submitHandler:function() {
            //$(form).ajaxSubmit();    //同步提交FORM
            var tname = $(‘input[name=tname]‘).val();
            var idcard = $(‘input[name=idcard]‘).val();
            var resultMsg = $(‘#resultMsg‘);
            ajaxSubmitForm(tname, idcard, resultMsg);            
        }
    });
})

//异步执行方法
function ajaxSubmitForm(tname, idcard, resultMsg) {
    $.ajax({
        url: "/member/nameauth",
        data: "tname=" + tname + "&idcard=" + idcard + "&con_idcard=" + idcard,
        type: "post",
        cache: false,
        dataType: "json",
        success: function(data, textStatus){
            if(data.return){
                window.location.reload();    
            }else{
                resultMsg.css({‘color‘: ‘red‘}).html(data.errmsg).show().delay(2000).hide(0);
            }
        }
    });
}

或者

$(function(){
    //jquery.validate插件自定义AJAX验证(验证账户是否存在)
    $.validator.addMethod("checkUserExist",function(value,element){
        var user = value;
        $.ajax({
            type:"POST",
            async:true, 
            dataType:‘json‘,
            url:"/account/verifyAccount",
            data:"param=" + $(‘input[name=newpassword]‘).val(),
            success:function(response){
                if(response){
                    res = response.state;
                }
            }
        });
        return res;
    },"账号不存在,请仔细检查!");
    
    //jquery.validate插件验证
    $("#signupForm").validate({
        rules: {
            newpassword: {
                required: true,
                rangelength:[6,30]
            }
        },
        messages: {
            newpassword: {
                required: "请输入6~30个字符的交易密码!",
                rangelength: "请输入6~30个字符的交易密码!"
            }
        },
        submitHandler:function(form) {
            $(form).ajaxSubmit();
        }
    });
})

 

jQuery Validate教程

http://www.w3cschool.cc/jquery/jquery-plugin-validate.html

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。