jquery validate自定义规则

//检查身份证号码是否存在
$.validator.addMethod("checkIDCardExist", function (value, element) {
    if ($("#IDType").val() == "1")//为身份证号码时
    {
        var IDCard = value,
       res = false;
        $.ajax({
            type: "POST",
            async: false,
            url: "/Server/CheckIDCard",
            data: { IDCard: IDCard },
            success: function (response) {
                if (response.isSuccess) {
                    //给form元素赋值
                    res = true;
                    databind(response);
                } else {
                    res = false;
                }
            }
        });
        return res;
    } else {
        return true;
    }

}, "身份证号码不存在");
//检查身份证号码格式
$.validator.addMethod("checkIDCardFormat", function (value, element) {
    if ($("#IDType").val() == "1")//为身份证号码时
    {
        var msg = ""
          , IDCardObj = {}
          , Birthday = ""
          , Sex = ""
          , Age = "";
        IDCardObj = new IDCard();
        msg = IDCardObj.CheckIdCard(value);
        if (msg != "验证通过!") {
            $.validator.messages.checkIDCardFormat = msg;
            return false;
        } else {
            //得到生日,性别,年龄
            Birthday = IDCardObj.GetBirthday(value);
            Sex = IDCardObj.GetSex(value);
            Age = IDCardObj.GetAge(value);
            $("#Birthday").val(Birthday);
            $("#Sex").val(Sex == "M" ? "true" : "false");
            $("#form_BasicInfo input[name='Age']").val(Age);
            return true;
        }
    } else {
        return true;
    }
});
//检查字符数量汉字(算两个字符)
$.validator.addMethod("checkNumber", function (value, element) {
    var num = 0;  //总个数累加判断  
    for (var i = 0; i < value.length; i++) {
        //根据charCodeAt来判断输入的是中文还是字母,符号    
        var charCode = value.charCodeAt(i);
        if (charCode >= 0 && charCode <= 128) {
            //字符就+1  
            num += 1;
        } else {
            //汉子就+2  
            num += 2;
        }
    };
    if (num > 5) {
        return false;
    } else {
        return true;
    }

}, "<font color='#E47068'>最多可以输入5个字符,汉字(算两个)</font>");


$.validator.addMethod("checkDateFormat", function (value, element, params) {
    var reg = /^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$/;
    if (this.optional(element) || reg.test(value)) {
        if (params[0] == true && reg.test(value)) {//计算年龄
            var Age = new IDCard().GetAgeByDate(value);
            $("input[name='Age']").val(Age);
        }
        return true;
    } else {
        return false;
    }
}, "时间格式有误");

//定义错误提示出现的位置
$.validator.setDefaults({
    errorPlacement: function (error, element) {//error为错误提示对象,element为出错的组件对象  
        if (element.parent(".date").size() > 0) {
            error.appendTo(element.parent().parent());
        } else {
            element.after(error);//默认是加在 输入框的后面。这个else必须写。不然其他非radio的组件 就无法显示错误信息了。  
        }
    }
});
原文地址:https://www.cnblogs.com/gaocong/p/7299020.html