/** * jQuery Validation Plugin 1.9.0 * * http://bassistance.de/jquery-plugins/jquery-plugin-validation/ * http://docs.jquery.com/Plugins/Validation * * Copyright (c) 2006 - 2011 Jörn Zaefferer * * Dual licensed under the MIT and GPL licenses: * http://www.opensource.org/licenses/mit-license.php * http://www.gnu.org/licenses/gpl.html */ (function() { function stripHtml(value) { // remove html tags and space chars return value.replace(/<.[^<>]*?>/g, ' ').replace(/ | /gi, ' ') // remove numbers and punctuation .replace(/[0-9.(),;:!?%#$'"_+=\/-]*/g,''); } jQuery.validator.addMethod("password", function( value, element ) { var result = this.optional(element) || value.length >= 8 && /\d/.test(value) && /[a-z]/i.test(value); if (!result) { //element.value = ""; var validator = this; setTimeout(function() { validator.blockFocusCleanup = true; element.focus(); validator.blockFocusCleanup = false; }, 1); } return result; }, "包含数字和字母且不少于8位数"); jQuery.validator.addMethod("maxWords", function(value, element, params) { return this.optional(element) || stripHtml(value).match(/\b\w+\b/g).length < params; }, jQuery.validator.format("输入不可超过 {0} 个字")); jQuery.validator.addMethod("minWords", function(value, element, params) { return this.optional(element) || stripHtml(value).match(/\b\w+\b/g).length >= params; }, jQuery.validator.format("输入至少 {0} 个字")); jQuery.validator.addMethod("rangeWords", function(value, element, params) { return this.optional(element) || stripHtml(value).match(/\b\w+\b/g).length >= params[0] && value.match(/bw+b/g).length < params[1]; }, jQuery.validator.format("输入 {0} 到 {1} 个字")); })(); jQuery.validator.addMethod("letterswithbasicpunc", function(value, element) { return this.optional(element) || /^[a-z-.,()'\"\s]+$/i.test(value); }, "仅限字母和标点符号"); jQuery.validator.addMethod("alphanumeric", function(value, element) { return this.optional(element) || /^\w+$/i.test(value); }, "仅限字母、数字、下划线"); jQuery.validator.addMethod("lettersonly", function(value, element) { return this.optional(element) || /^[a-z]+$/i.test(value); }, "仅限字母"); jQuery.validator.addMethod("nowhitespace", function(value, element) { return this.optional(element) || /^\S+$/i.test(value); }, "不支持空格"); jQuery.validator.addMethod("time", function(value, element) { return this.optional(element) || /^([01]\d|2[0-3])(:[0-5]\d){0,2}$/.test(value); }, "请输入正确的时间, 在 00:00 到 23:59 之间"); jQuery.validator.addMethod("time12h", function(value, element) { return this.optional(element) || /^((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))$/i.test(value); }, "请输入正确的时间,在 00:00 到12:00 之间"); // TODO check if value starts with <, otherwise don't try stripping anything jQuery.validator.addMethod("strippedminlength", function(value, element, param) { return jQuery(value).text().length >= param; }, jQuery.validator.format("Please enter at least {0} characters")); // same as email, but TLD is optional jQuery.validator.addMethod("email2", function(value, element, param) { return this.optional(element) || /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)*(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(value); }, jQuery.validator.messages.email); // same as url, but TLD is optional jQuery.validator.addMethod("url2", function(value, element, param) { return this.optional(element) || /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)*(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(value); }, jQuery.validator.messages.url); // 域名或者IP地址 jQuery.validator.addMethod("domainORip", function(value, element) { return this.optional(element) || /^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$/.test(value) || /^(2[0-5]{2}|2[0-4][0-9]|1?[0-9]{1,2}).(2[0-5]{2}|2[0-4][0-9]|1?[0-9]{1,2}).(2[0-5]{2}|2[0-4][0-9]|1?[0-9]{1,2}).(2[0-5]{2}|2[0-4][0-9]|1?[0-9]{1,2})$/.test(value) ; }, "请正确输入域名或IP地址格式"); // 登录名 jQuery.validator.addMethod("strUserName", function(value, element) { return this.optional(element) || /^\w+$/.test(value); }, "账户名包括英文字母、数字和下划线"); // 字符验证 jQuery.validator.addMethod("stringCheck", function(value, element) { return this.optional(element) || /^[\u0391-\uFFE5\w]+$/.test(value); }, "只能包括中文字、英文字母、数字和下划线"); // 中文字两个字节 jQuery.validator.addMethod("byteRangeLength", function(value, element, param) { var length = value.length; for(var i = 0; i < value.length; i++){ if(value.charCodeAt(i) > 127){ length++; } } return this.optional(element) || ( length >= param[0] && length <= param[1] ); }, "请确保输入的值在3-15个字节之间(一个中文字算2个字节)"); // 身份证号码验证 jQuery.validator.addMethod("isIdCardNo", function(value, element) { return this.optional(element) || idCardNoUtil.checkIdCardNo(value); }, "请正确输入身份证号码"); //护照编号验证 jQuery.validator.addMethod("passport", function(value, element) { return this.optional(element) || checknumber(value); }, "请正确输入护照编号"); // 手机号码验证 jQuery.validator.addMethod("isMobile", function(value, element) { var length = value.length; var mobile = /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(18[0-9]{1}))+\d{8})$/; return this.optional(element) || (length == 11 && mobile.test(value)); }, "请正确填写手机号码"); // 电话号码验证 jQuery.validator.addMethod("isTel", function(value, element) { var tel = /^\d{3,4}-?\d{7,9}$/; //电话号码格式010-12345678 return this.optional(element) || (tel.test(value)); }, "请正确填写电话号码"); // 联系电话(手机/电话皆可)验证 jQuery.validator.addMethod("isPhone", function(value,element) { var length = value.length; var mobile = /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(18[0-9]{1}))+\d{8})$/; var tel = /^\d{3,4}-?\d{7,9}$/; return this.optional(element) || (tel.test(value) || mobile.test(value)); }, "请正确填写联系电话"); // 邮政编码验证 jQuery.validator.addMethod("isZipCode", function(value, element) { var zc = /^[0-9]{6}$/; return this.optional(element) || (zc.test(value)); }, "请正确填写邮政编码"); // 域名 jQuery.validator.addMethod("isDomain", function(value, element) { var domain = /^([\w.]+)$/; return this.optional(element) || (domain.test(value)); }, "请正确填写域名"); // 同时满足手机号码或电子邮箱 jQuery.validator.addMethod("isMobileOrEmail", function(value,element) { var length = value.length; var mobile = /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(18[0-9]{1}))+\d{8})$/; var mail = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/; return this.optional(element) || (mail.test(value) || mobile.test(value)); }, "请输入正确的手机或邮箱"); // NOTICE: Modified version of Castle.Components.Validator.CreditCardValidator // Redistributed under the the Apache License 2.0 at http://www.apache.org/licenses/LICENSE-2.0 // Valid Types: mastercard, visa, amex, dinersclub, enroute, discover, jcb, unknown, all (overrides all other settings) jQuery.validator.addMethod("creditcardtypes", function(value, element, param) { if (/[^0-9-]+/.test(value)) return false; value = value.replace(/\D/g, ""); var validTypes = 0x0000; if (param.mastercard) validTypes |= 0x0001; if (param.visa) validTypes |= 0x0002; if (param.amex) validTypes |= 0x0004; if (param.dinersclub) validTypes |= 0x0008; if (param.enroute) validTypes |= 0x0010; if (param.discover) validTypes |= 0x0020; if (param.jcb) validTypes |= 0x0040; if (param.unknown) validTypes |= 0x0080; if (param.all) validTypes = 0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080; if (validTypes & 0x0001 && /^(51|52|53|54|55)/.test(value)) { //mastercard return value.length == 16; } if (validTypes & 0x0002 && /^(4)/.test(value)) { //visa return value.length == 16; } if (validTypes & 0x0004 && /^(34|37)/.test(value)) { //amex return value.length == 15; } if (validTypes & 0x0008 && /^(300|301|302|303|304|305|36|38)/.test(value)) { //dinersclub return value.length == 14; } if (validTypes & 0x0010 && /^(2014|2149)/.test(value)) { //enroute return value.length == 15; } if (validTypes & 0x0020 && /^(6011)/.test(value)) { //discover return value.length == 16; } if (validTypes & 0x0040 && /^(3)/.test(value)) { //jcb return value.length == 16; } if (validTypes & 0x0040 && /^(2131|1800)/.test(value)) { //jcb return value.length == 15; } if (validTypes & 0x0080) { //unknown return true; } return false; }, "请输入正确的信用卡号"); jQuery.validator.addMethod("ipv4", function(value, element, param) { return this.optional(element) || /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/i.test(value); }, "请输入正确的IPV4类型的IP地址"); jQuery.validator.addMethod("ipv6", function(value, element, param) { return this.optional(element) || /^((([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){0,5}:((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(::([0-9A-Fa-f]{1,4}:){0,5}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|(::([0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:))$/i.test(value); }, "请输入正确的IPV6类型的IP地址"); /** * Return true if the field value matches the given format RegExp * * @example jQuery.validator.methods.pattern("AR1004",element,/^AR\d{4}$/) * @result true * * @example jQuery.validator.methods.pattern("BR1004",element,/^AR\d{4}$/) * @result false * * @name jQuery.validator.methods.pattern * @type Boolean * @cat Plugins/Validate/Methods */ jQuery.validator.addMethod("pattern", function(value, element, param) { return this.optional(element) || param.test(value); }, "错误格式"); jQuery.validator.addMethod("isDate", function(value, element){ var ereg = /^(\d{1,4})(-|\/)(\d{1,2})(-|\/)(\d{1,2})$/; var r = value.match(ereg); if (r == null) { return false; } var d = new Date(r[1], r[3] - 1, r[5]); var result = (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[5]); return this.optional(element) || (result); }, "请输入正确的日期");