jQuery Validate
jQuery Validate 插件为表单提供了强大的验证功能,让客户端表单验证变得更简单,同时提供了大量的定制选项,满足应用程序各种需求。该插件捆绑了一套有用的验证方法,包括 URL 和电子邮件验证,同时提供了一个用来编写用户自定义方法的 API。所有的捆绑方法默认使用英语作为错误信息,且已翻译成其他 37 种语言。
该插件是由 Jörn Zaefferer 编写和维护的,他是 jQuery 团队的一名成员,是 jQuery UI 团队的主要开发人员,是 QUnit 的维护人员。该插件在 2006 年 jQuery 早期的时候就已经开始出现,并一直更新至今。
}, "只能包括中文字、英文字母、数字和下划线");// 中文字两个字节
jQuery.validator.addMethod("byteRangeLength", function(value, element, param) {var length &#61; value.length;for (var i &#61; 0; i 127) {length&#43;&#43;;}}return this.optional(element) || (length >&#61; param[0] && length <&#61; param[1]);
}, "请确保输入的值在3 - 15个字节之间(一个中文字算2个字节)");// 身份证号码验证
jQuery.validator.addMethod("isIdCardNo", function(value, element) {return this.optional(element) || isIdCardNo(value);
}, "请正确输入您的身份证号码");// 手机号码验证
jQuery.validator.addMethod("isMobile", function(value, element) {var length &#61; value.length;var mobile &#61; /^(((13[0-9]{1})|(15[0-9]{1}))&#43;d{8})$/;return this.optional(element) || (length &#61;&#61; 11 && mobile.test(value));
}, "请正确填写您的手机号码");// 电话号码验证
jQuery.validator.addMethod("isTel", function(value, element) {var tel &#61; /^d{3,4}-?d{7,9}$/; //电话号码格式010-12345678return this.optional(element) || (tel.test(value));
}, "请正确填写您的电话号码");// 联系电话(手机/电话皆可)验证
jQuery.validator.addMethod("isPhone", function(value, element) {var length &#61; value.length;var mobile &#61; /^(((13[0-9]{1})|(15[0-9]{1}))&#43;d{8})$/;var tel &#61; /^d{3,4}-?d{7,9}$/;return this.optional(element) || (tel.test(value) || mobile.test(value));
}, "请正确填写您的联系电话");// 邮政编码验证
jQuery.validator.addMethod("isZipCode", function(value, element) {var tel &#61; /^[0-9]{6}$/;return this.optional(element) || (tel.test(value));
}, "请正确填写您的邮政编码");function isIdCardNo(num) {var factorArr &#61; newArray(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1);var parityBit &#61; newArray("1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2");var varArray &#61; new Array();var intValue;var lngProduct &#61; 0;var intCheckDigit;var intStrLen &#61; num.length;var idNumber &#61; num;// initializeif ((intStrLen !&#61; 15) && (intStrLen !&#61; 18)) {return false;}// check and set valuefor (i &#61; 0; i &#39;9&#39;) && (i !&#61; 17)) {return false;} else if (i <17) {varArray[i] &#61; varArray[i] * factorArr[i];}}if (intStrLen &#61;&#61; 18) {//check datevar date8 &#61; idNumber.substring(6, 14);if (isDate8(date8) &#61;&#61; false) {return false;}// calculate the sum of the productsfor (i &#61; 0; i <17; i&#43;&#43;) {lngProduct &#61; lngProduct &#43; varArray[i];}// calculate the check digitintCheckDigit &#61; parityBit[lngProduct % 11];// check last digitif (varArray[17] !&#61; intCheckDigit) {return false;}} else { //length is 15//check datevar date6 &#61; idNumber.substring(6, 12);if (isDate6(date6) &#61;&#61; false) {return false;}}return true;
}function isDate6(sDate) {if (!/^[0-9]{6}$/.test(sDate)) {return false;}var year, month, day;year &#61; sDate.substring(0, 4);month &#61; sDate.substring(4, 6);if (year <1700 || year > 2500) return falseif (month <1 || month > 12) returnfalsereturn true
}function isDate8(sDate) {if (!/^[0-9]{8}$/.test(sDate)) {return false;}var year, month, day;year &#61; sDate.substring(0, 4);month &#61; sDate.substring(4, 6);day &#61; sDate.substring(6, 8);var iaMonthDays &#61; [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]if (year <1700 || year > 2500) return falseif (((year % 4 &#61;&#61; 0) && (year % 100 !&#61; 0)) || (year % 400 &#61;&#61; 0)) iaMonthDays[1] &#61; 29;if (month <1 || month > 12) returnfalseif (day <1 || day > iaMonthDays[month - 1]) return falsereturn true
}//身份证号码验证
jQuery.validator.addMethod("idcardno", function(value, element) {return this.optional(element) || isIdCardNo(value);
}, "请正确输入身份证号码");//字母数字
jQuery.validator.addMethod("alnum", function(value, element) {return this.optional(element) || /^[a-zA-Z0-9]&#43;$/.test(value);
}, "只能包括英文字母和数字");// 汉字
jQuery.validator.addMethod("chcharacter", function(value, element) {var tel &#61; /^[u4e00-u9fa5]&#43;$/;return this.optional(element) || (tel.test(value));
}, "请输入汉字");// 字符最小长度验证&#xff08;一个中文字符长度为2&#xff09;
jQuery.validator.addMethod("stringMinLength", function(value, element, param) {var length &#61; value.length;for (var i &#61; 0; i 127) {length&#43;&#43;;}}return this.optional(element) || (length >&#61; param);
}, $.validator.format("长度不能小于{0}!"));// 字符最大长度验证&#xff08;一个中文字符长度为2&#xff09;
jQuery.validator.addMethod("stringMaxLength", function(value, element, param) {var length &#61; value.length;for (var i &#61; 0; i 127) {length&#43;&#43;;}}return this.optional(element) || (length <&#61; param);
}, $.validator.format("长度不能大于{ 0}!"));// 字符验证
jQuery.validator.addMethod("string", function(value, element) {return this.optional(element) || /^[u0391-uFFE5w]&#43;$/.test(value);
}, "不允许包含特殊符号!");// 手机号码验证
jQuery.validator.addMethod("mobile", function(value, element) {var length &#61; value.length;return this.optional(element) || (length &#61;&#61; 11 && /^(((13[0-9]{1})|(15[0-9]{1}))&#43;d{8})$/.test(value));
}, "手机号码格式错误!");// 电话号码验证
jQuery.validator.addMethod("phone", function(value, element) {var tel &#61; /^(d{3,4}-?)?d{7,9}$/g;return this.optional(element) || (tel.test(value));
}, "电话号码格式错误!");// 邮政编码验证
jQuery.validator.addMethod("zipCode", function(value, element) {var tel &#61; /^[0-9]{6}$/;return this.optional(element) || (tel.test(value));
}, "邮政编码格式错误!");// 必须以特定字符串开头验证
jQuery.validator.addMethod("begin", function(value, element, param) {var begin &#61; new RegExp("^"&#43;param);return this.optional(element) || (begin.test(value));
}, $.validator.format("必须以{0}开头!"));// 验证两次输入值是否不相同
jQuery.validator.addMethod("notEqualTo", function(value, element, param) {return value !&#61; $(param).val();
}, $.validator.format("两次输入不能相同!"));//验证值不允许与特定值等于
jQuery.validator.addMethod("notEqual", function(value, element, param) {return value !&#61; param;
}, $.validator.format("输入值不允许为{0}!"));// 验证值必须大于特定值(不能等于)
jQuery.validator.addMethod("gt", function(value, element, param) {return value > param;
}, $.validator.format(“输入值必须大于{0}!"));