jQuery Validate验证

//扩展验证规则

//邮箱 表单验证规则jQuery.validator.addMethod("mail",function(value, element){varmail =/^[a-z0-9._%-]+@([a-z0-9-]+\.)+[a-z]{2,4}$/;returnthis.optional(element) || (mail.test(value));},"邮箱格式不对");

//电话验证规则jQuery.validator.addMethod("phone",function(value, element){varphone =/^0\d{2,3}-\d{7,8}$/;returnthis.optional(element) || (phone.test(value));},"电话格式如:0371-68787027");

//区号验证规则jQuery.validator.addMethod("ac",function(value, element){varac =/^0\d{2,3}$/;returnthis.optional(element) || (ac.test(value));},"区号如:010或0371");

//无区号电话验证规则jQuery.validator.addMethod("noactel",function(value, element){varnoactel =/^\d{7,8}$/;returnthis.optional(element) || (noactel.test(value));},"电话格式如:68787027");

//手机验证规则jQuery.validator.addMethod("mobile",function(value, element){varmobile =/^1[3|4|5|7|8]\d{9}$/;returnthis.optional(element) || (mobile.test(value));},"手机格式不对");

//邮箱或手机验证规则jQuery.validator.addMethod("mm",function(value, element){varmm =/^[a-z0-9._%-]+@([a-z0-9-]+\.)+[a-z]{2,4}$|^1[3|4|5|7|8]\d{9}$/;returnthis.optional(element) || (mm.test(value));},"格式不对");

//电话或手机验证规则jQuery.validator.addMethod("tm",function(value, element){vartm=/(^1[3|4|5|7|8]\d{9}$)|(^\d{3,4}-\d{7,8}$)|(^\d{7,8}$)|(^\d{3,4}-\d{7,8}-\d{1,4}$)|(^\d{7,8}-\d{1,4}$)/;returnthis.optional(element) || (tm.test(value));},"格式不对")

;//年龄 表单验证规则

jQuery.validator.addMethod("age",function(value, element){varage =/^(?:[1-9][0-9]?|1[01][0-9]|120)$/;returnthis.optional(element) || (age.test(value));},"不能超过120岁");///// 20-60  /^([2-5]\d)|60$/

//传真

jQuery.validator.addMethod("fax",function(value,element){varfax =/^(\d{3,4})?[-]?\d{7,8}$/;returnthis.optional(element) || (fax.test(value));},"传真格式如:0371-68787027");

//验证当前值和目标val的值相等 相等返回为 false

jQuery.validator.addMethod("equalTo2",function(value, element){varreturnVal =true;varid = $(element).attr("data-rule-equalto2");vartargetVal = $(id).val();if(value === targetVal){        returnVal =false;    }returnreturnVal;},"不能和原始密码相同");//大于指定数jQuery.validator.addMethod("gt",function(value, element){varreturnVal =false;vargt = $(element).data("gt");if(value > gt && value !=""){        returnVal =true;    }returnreturnVal;},"不能小于0 或空");//汉字jQuery.validator.addMethod("chinese",function(value, element){varchinese =/^[\u4E00-\u9FFF]+$/;returnthis.optional(element) || (chinese.test(value));},"格式不对");

//指定数字的整数倍

jQuery.validator.addMethod("times",function(value, element){varreturnVal =true;varbase=$(element).attr('data-rule-times');if(value%base!=0){        returnVal=false;    }returnreturnVal;},"必须是发布赏金的整数倍");

//身份证jQuery.validator.addMethod("idCard",function(value, element){varisIDCard1=/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/;//(15位)varisIDCard2=/^[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)$/;//(18位)returnthis.optional(element) || (isIDCard1.test(value)) || (isIDCard2.test(value));},"格式不对";

// 字符验证

jQuery.validator.addMethod("stringCheck",function(value, element){returnthis.optional(element) ||/^[\u0391-\uFFE5\w]+$/.test(value);        },"只能包括中文字、英文字母、数字和下划线");  ------------------------------------------------------------

// 中文字两个字节

jQuery.validator.addMethod("byteRangeLength",function(value, element, param){varlength = value.length;for(vari =0; i < value.length; i++){if(value.charCodeAt(i) >127){                length++;                }          }returnthis.optional(element) || ( length >= param[0] && length <= param[1] );        },"请确保输入的值在3-15个字节之间(一个中文字算2个字节)");  ------------------------------------------------------------

// 身份证号码验证jQuery.validator.addMethod("isIdCardNo",function(value, element){returnthis.optional(element) || isIdCardNo(value);      },"请正确输入您的身份证号码");------------------------------------------------------------

// 手机号码验证jQuery.validator.addMethod("isMobile",function(value, element){varlength = value.length;varmobile =/^[1][3-8]+\\d{9}/;returnthis.optional(element) || (length ==11&& mobile.test(value));      },"请正确填写您的手机号码");      ------------------------------------------------------------

// 电话号码验证jQuery.validator.addMethod("isTel",function(value, element){vartel =/^\d{3,4}-?\d{7,9}$/;//电话号码格式010-12345678returnthis.optional(element) || (tel.test(value));      },"请正确填写您的电话号码");  ------------------------------------------------------------

// 联系电话(手机/电话皆可)验证jQuery.validator.addMethod("isPhone",function(value,element){varlength = value.length;varmobile =/^(((13[0-9]{1})|(15[0-9]{1}))+\d{8})$/;vartel =/^\d{3,4}-?\d{7,9}$/;returnthis.optional(element) || (tel.test(value) || mobile.test(value));    },"请正确填写您的联系电话");  ------------------------------------------------------------

// 邮政编码验证jQuery.validator.addMethod("isZipCode",function(value, element){vartel =/^[0-9]{6}$/;returnthis.optional(element) || (tel.test(value));        },"请正确填写您的邮政编码");

你可能感兴趣的:(jQuery Validate验证)