diff --git a/README.md b/README.md index f52556b..7696c1c 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # jQuery Validator -当前版本: **1.3.7** +当前版本: **1.3.8** jQuery Validator 是一个简单易用、功能强大的基于jQuery的表单验证框架。其设计目标如下: * 【简单易用,轻松上手】:一般情况下,无需进行任何配置,内置的校验器和校验规则即可满足绝大多数中文环境下的常规表单验证需求,开箱即可使用! @@ -11,6 +11,6 @@ jQuery Validator 是一个简单易用、功能强大的基于jQuery的表单验 ## jQuery Validator 依赖 jQuery 1.6+(建议采用 1.x 或 2.x 的最新版本) ## jQuery Validator 初体验 & 快速入门 -演示示例及快速入门教程,请参见网址:[http://www.365mini.com/page/jquery-validator-quickstart.htm](http://www.365mini.com/page/jquery-validator-quickstart.htm "jQuery Validator 快速入门")。 +演示示例及快速入门教程,请参见网址:[https://codeplayer.vip/p/j7sud](https://codeplayer.vip/p/j7sud "jQuery Validator 快速入门")。 ## jQuery Validator 文档 -请参见网址:[http://www.365mini.com/page/tag/jquery-validator](http://www.365mini.com/page/tag/jquery-validator "jQuery Validator 文档")。 +请参见网址:[https://codeplayer.vip/p/j7sus](https://codeplayer.vip/p/j7sus "jQuery Validator 文档")。 \ No newline at end of file diff --git a/jquery-validator.js b/jquery-validator.js index c07bb93..51b0c34 100644 --- a/jquery-validator.js +++ b/jquery-validator.js @@ -1,8 +1,8 @@ /************************************** @Name: jQuery-Validator 基于jQuery的前端验证框架 - @Version: 1.3.7 + @Version: 1.3.8 @Author: Ready - @Date: 2020-08-24 + @Date: 2021-11-12 @Documentation: https://codeplayer.vip/p/j7sud @Email: CodePlayer360@gmail.com @Licence: https://www.apache.org/licenses/LICENSE-2.0.html @@ -36,7 +36,7 @@ }, fn = V.fn = V.prototype = { // 版本号 - version: "1.3.6", + version: "1.3.8", constructor: V, @@ -694,11 +694,12 @@ } }, // 渲染错误 - renderError: function(message, $target, context){ - if($target && $target.length && $.isFunction($target.tips)){ - $target.tips(message); - }else { - alert(message); + renderError: function(msg, $target, context){ + if($target && $target.length && $.isFunction($target.tips) && $target.is(':visible')){ + // 只有可见的元素才适合使用 tips 方式渲染错误 + $target.tips(msg); + } else { + $.isFunction($.alert) ? $.alert(msg) : alert(msg); } var e = context.event; if( !e || e.type != "focusout" && e.type != "blur") @@ -786,7 +787,7 @@ getLabel: function(name, $dom, context){ if(!name && $dom) name = $dom.prop("name"); if(name != null){ - var label = this.labels[name] || fn.labels[name]; + var label = context.label || this.labels[name] || fn.labels[name]; if(label == null && $dom && $dom.length ){ label = $dom.attr(this.labelAttr || "label") || $dom.first().prev("label").text(); } diff --git a/jquery-validator.min.js b/jquery-validator.min.js index a5f8da7..533c118 100644 --- a/jquery-validator.min.js +++ b/jquery-validator.min.js @@ -1,10 +1,10 @@ /************************************** @Name: jQuery-Validator 基于jQuery的前端验证框架 - @Version: 1.3.7 + @Version: 1.3.8 @Author: Ready - @Date: 2020-08-24 - @Documentation: https://codeplayer.vip/p/j7sud - @Email: CodePlayer360@gmail.com + @Date: 2021-11-12 + @Documentation: https://codeplayer.vip/p/j7sud + @Email: codeplayer@foxmail.com @Licence: https://www.apache.org/licenses/LICENSE-2.0.html */ -!function(a,b){var d,e,f,g,c=b&&b.console;return"undefined"==typeof jQuery||a!==jQuery?(c&&c.log("jQuery must be initialized before loading the Validator."),void 0):(d=function(b){if(!(this instanceof d))return new d(b);if(b||(b="v"),!a.fn[b]){var c=this;a.fn[b]=function(a,b){return c.execute(this,a,b)}}},e=d.cache={},f=d.log=function(){if(c){for(var a=0;a2&&(g=["?",g[0]])):g=["?",null],e[c.rule.format]=g),d.debug&&f("idcard pattern:",g),!a||"?"==g[0]&&15!=a.length&&18!=a.length||a.length!=g[0])return!1;if(h=18==a.length){for(i=[7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2],j=0,k=0;kq.getTime())return!1;if(r=q.getFullYear()-p.getFullYear(),q.setFullYear(q.getFullYear()-r),p.getTime()>q.getTime()&&r--,s=c.label,c.age=r,c.label="身份证年龄",this.validator.range.call(this,r,g[1],c)===!1)return!1;c.label=s}return p}},validator:{required:function(b,c,d){var f,g,e=d.$dom;if(e&&e.length&&(g=e[0],g.nodeName,null==g.checked||"checkbox"!=g.type&&"radio"!=g.type||(f=!0,d.actual=b=e.filter(":checked").length)),!b){if(c&&(!a.isFunction(c)||(c=c.call(this,b,d))))return this.sendError(f?"checked":"",b,"required",d),!1;d._stop=!0}},format:function(a,b,c){var e=d.util.parseFormat(b),f=this.formatter[c.child=e[0]];if(f)return a=c.formatResult=f.call(this,a,e[1],c),a===!1&&this.sendError(b,a,e[1],c),a;throw"Unsupported format validator:"+e[0]},text:function(b,c,d){var e=a.type(c);switch(e){case"string":if(c!=b)return this.sendError("==",b,c,d),!1;break;case"regexp":if(!c.test(b))return this.sendError("regexp",b,c,d),!1;break;case"function":if(c.call(this,b,d)===!1)return this.sendError("function",b,c,d),!1;break;default:throw"Unexpected text validator:"+c}},length:function(a,b,c){return this.validator.range.call(this,c.value.length,b,c)},range:function(a,b,c){var f,g,e=d.util.parseIntervalPattern(b);if(e.equals){if(a!=e.min)return this.sendError("==",a,e.min,c),!1}else{if(e.min&&(f=a>e.min||">="==e.left&&e.min==a,!f))return this.sendError(e.left,a,e.min,c),f;if(e.max&&(g=a=":"{label}必须大于或等于{expected}!",">":"{label}必须大于{expected}!","<=":"{label}必须小于或等于{expected}!","<":"{label}必须小于{expected}!","length.==":"{label}的长度必须等于{expected}!","length.>=":"{label}的长度必须大于或等于{expected}!","length.>":"{label}的长度必须大于{expected}!","length.<=":"{label}的长度必须小于或等于{expected}!","length.<":"{label}的长度必须小于{expected}!","format.number":"{label}必须是有效的整数!","format.number/money":"{label}必须是整数或最多保留两位的小数!","format.number/double":"{label}必须是有效的整数或小数!","format.date":'{label}必须为"{dateFormat}"格式的有效时间!',file:"{label}的格式不正确,必须为{expected}等格式!","default":"{label}的格式不正确!"},setMessage:function(a,b,c){c||(c=!(this instanceof d));var e=c?g.messages:this.messages;if(c||e!=g.messages||(this.messages=e={}),"object"==typeof a)for(i in a)e[i]=a[i];else e[a]=b},getMessage:function(b){var e,f,h,c=[b.message,b.rule.message,this.messages,g.messages];for(h in c){if(f=e=c[h],e===!1)return f;if(null!=e){if("object"==typeof e&&(f=e[b.validator+"."+b.trigger],null==f&&(!b.child||null==(f=e[b.validator+"."+b.child]))&&null==(f=e[b.validator])&&(f=e[b.trigger])),f===!1)return f;if(null!=f)break}}return f&&a.isFunction(f)&&(f=f.call(null,b)),f!==!1&&(f||(f=this.messages["default"]||g.messages["default"]),f&&(f=d.util.parseMessage(f,b,this))),f},labels:{},setLabel:function(a,b,c){c||(c=!(this instanceof d));var e=c?g.labels:this.labels;if(c||e!=g.labels||(this.labels=e={}),"object"==typeof a)for(i in a)e[i]=a[i];else e[a]=b},getLabel:function(a,b){if(!a&&b&&(a=b.prop("name")),null!=a){var d=this.labels[a]||g.labels[a];if(null==d&&b&&b.length&&(d=b.attr(this.labelAttr||"label")||b.first().prev("label").text()),d)return d}return""}},d.util={parseFormat:function(a){var b=a.indexOf("/"),c=[a,""];return b>0&&(c[0]=a.substring(0,b),c[1]=a.substr(b+1)),c},parseIntervalPattern:function(a){if(!e[a]){if(e.__interval||(e.__interval=/^([\[\(])(-?\d+(\.\d+)?)?(,)?(-?\d+(\.\d+)?)?([\]\)])$/),!e.__interval.test(a)||!RegExp.$2&&!RegExp.$4)throw"Unexpected interval pattern:"+a;var b={min:RegExp.$2,max:RegExp.$5};RegExp.$4==RegExp.$5?b.equals=!0:(b.left="["==RegExp.$1?">=":">",b.right="]"==RegExp.$7?"<=":"<"),e[a]=b}return e[a]},parseMessage:function(b,c,d){var f=c.$relatedDoms;return b.replace(e.__message||(e.__message=/\{([^}]+)\}/g),function(b,e){var h,i,g=e.charAt(0);return"#"==g||"$"==g?(i=e.substr(1),h=f&&f[i]?d.getLabel(null,f[i],c):d.getLabel(null,a("#"==g?"#"+i:"[name='"+i+"']"),c)):h=c[e],h||(h=""),h})},pushDomContext:function(a,b,c){var d=c.$relatedDoms||(c.$relatedDoms=[]);a||(a=b.prop("name")),d.push(b),a&&(d[a]=b)}},g.define({username:{pre:"trimAll,lower,flush",text:/^[a-z][a-z0-9_]{5,15}$/i,message:"{label}必须是英文字母、数字和下划线组成的6~16位字符,并且必须以字母开头!"},password:{length:"[6,16]"},age:{format:"number",range:"[18,100]"},"int":{format:"number"},"int+":{format:"number",range:"(0,)"},money:{format:"number/money"},"money+":{format:"number/money",range:"(0,)"},date:{format:"date"},datetime:{format:"date/datetime"},phoneCode:{format:"number",length:"[6]"},imageCode:{length:"[4]"},required:{},cellphone:!0,email:!0,idcard:{pre:"upper,flush",format:"idcard/18:[18,]"},image:"jpg|jpeg|gif|png|bmp"}),(b||(b="undefined"!=typeof window?window:this))&&(b.V=d),void 0)}(jQuery,window); \ No newline at end of file +!function(a,b){var d,e,f,g,c=b&&b.console;return"undefined"==typeof jQuery||a!==jQuery?(c&&c.log("jQuery must be initialized before loading the Validator."),void 0):(d=function(b){if(!(this instanceof d))return new d(b);if(b||(b="v"),!a.fn[b]){var c=this;a.fn[b]=function(a,b){return c.execute(this,a,b)}}},e=d.cache={},f=d.log=function(){if(c){for(var a=0;a2&&(g=["?",g[0]])):g=["?",null],e[c.rule.format]=g),d.debug&&f("idcard pattern:",g),!a||"?"==g[0]&&15!=a.length&&18!=a.length||a.length!=g[0])return!1;if(h=18==a.length){for(i=[7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2],j=0,k=0;kq.getTime())return!1;if(r=q.getFullYear()-p.getFullYear(),q.setFullYear(q.getFullYear()-r),p.getTime()>q.getTime()&&r--,s=c.label,c.age=r,c.label="身份证年龄",this.validator.range.call(this,r,g[1],c)===!1)return!1;c.label=s}return p}},validator:{required:function(b,c,d){var f,g,e=d.$dom;if(e&&e.length&&(g=e[0],g.nodeName,null==g.checked||"checkbox"!=g.type&&"radio"!=g.type||(f=!0,d.actual=b=e.filter(":checked").length)),!b){if(c&&(!a.isFunction(c)||(c=c.call(this,b,d))))return this.sendError(f?"checked":"",b,"required",d),!1;d._stop=!0}},format:function(a,b,c){var e=d.util.parseFormat(b),f=this.formatter[c.child=e[0]];if(f)return a=c.formatResult=f.call(this,a,e[1],c),a===!1&&this.sendError(b,a,e[1],c),a;throw"Unsupported format validator:"+e[0]},text:function(b,c,d){var e=a.type(c);switch(e){case"string":if(c!=b)return this.sendError("==",b,c,d),!1;break;case"regexp":if(!c.test(b))return this.sendError("regexp",b,c,d),!1;break;case"function":if(c.call(this,b,d)===!1)return this.sendError("function",b,c,d),!1;break;default:throw"Unexpected text validator:"+c}},length:function(a,b,c){return this.validator.range.call(this,c.value.length,b,c)},range:function(a,b,c){var f,g,e=d.util.parseIntervalPattern(b);if(e.equals){if(a!=e.min)return this.sendError("==",a,e.min,c),!1}else{if(e.min&&(f=a>e.min||">="==e.left&&e.min==a,!f))return this.sendError(e.left,a,e.min,c),f;if(e.max&&(g=a=":"{label}必须大于或等于{expected}!",">":"{label}必须大于{expected}!","<=":"{label}必须小于或等于{expected}!","<":"{label}必须小于{expected}!","length.==":"{label}的长度必须等于{expected}!","length.>=":"{label}的长度必须大于或等于{expected}!","length.>":"{label}的长度必须大于{expected}!","length.<=":"{label}的长度必须小于或等于{expected}!","length.<":"{label}的长度必须小于{expected}!","format.number":"{label}必须是有效的整数!","format.number/money":"{label}必须是整数或最多保留两位的小数!","format.number/double":"{label}必须是有效的整数或小数!","format.date":'{label}必须为"{dateFormat}"格式的有效时间!',file:"{label}的格式不正确,必须为{expected}等格式!","default":"{label}的格式不正确!"},setMessage:function(a,b,c){c||(c=!(this instanceof d));var e=c?g.messages:this.messages;if(c||e!=g.messages||(this.messages=e={}),"object"==typeof a)for(i in a)e[i]=a[i];else e[a]=b},getMessage:function(b){var e,f,h,c=[b.message,b.rule.message,this.messages,g.messages];for(h in c){if(f=e=c[h],e===!1)return f;if(null!=e){if("object"==typeof e&&(f=e[b.validator+"."+b.trigger],null==f&&(!b.child||null==(f=e[b.validator+"."+b.child]))&&null==(f=e[b.validator])&&(f=e[b.trigger])),f===!1)return f;if(null!=f)break}}return f&&a.isFunction(f)&&(f=f.call(null,b)),f!==!1&&(f||(f=this.messages["default"]||g.messages["default"]),f&&(f=d.util.parseMessage(f,b,this))),f},labels:{},setLabel:function(a,b,c){c||(c=!(this instanceof d));var e=c?g.labels:this.labels;if(c||e!=g.labels||(this.labels=e={}),"object"==typeof a)for(i in a)e[i]=a[i];else e[a]=b},getLabel:function(a,b,c){if(!a&&b&&(a=b.prop("name")),null!=a){var d=c.label||this.labels[a]||g.labels[a];if(null==d&&b&&b.length&&(d=b.attr(this.labelAttr||"label")||b.first().prev("label").text()),d)return d}return""}},d.util={parseFormat:function(a){var b=a.indexOf("/"),c=[a,""];return b>0&&(c[0]=a.substring(0,b),c[1]=a.substr(b+1)),c},parseIntervalPattern:function(a){if(!e[a]){if(e.__interval||(e.__interval=/^([\[\(])(-?\d+(\.\d+)?)?(,)?(-?\d+(\.\d+)?)?([\]\)])$/),!e.__interval.test(a)||!RegExp.$2&&!RegExp.$4)throw"Unexpected interval pattern:"+a;var b={min:RegExp.$2,max:RegExp.$5};RegExp.$4==RegExp.$5?b.equals=!0:(b.left="["==RegExp.$1?">=":">",b.right="]"==RegExp.$7?"<=":"<"),e[a]=b}return e[a]},parseMessage:function(b,c,d){var f=c.$relatedDoms;return b.replace(e.__message||(e.__message=/\{([^}]+)\}/g),function(b,e){var h,i,g=e.charAt(0);return"#"==g||"$"==g?(i=e.substr(1),h=f&&f[i]?d.getLabel(null,f[i],c):d.getLabel(null,a("#"==g?"#"+i:"[name='"+i+"']"),c)):h=c[e],h||(h=""),h})},pushDomContext:function(a,b,c){var d=c.$relatedDoms||(c.$relatedDoms=[]);a||(a=b.prop("name")),d.push(b),a&&(d[a]=b)}},g.define({username:{pre:"trimAll,lower,flush",text:/^[a-z][a-z0-9_]{5,15}$/i,message:"{label}必须是英文字母、数字和下划线组成的6~16位字符,并且必须以字母开头!"},password:{length:"[6,16]"},age:{format:"number",range:"[18,100]"},"int":{format:"number"},"int+":{format:"number",range:"(0,)"},money:{format:"number/money"},"money+":{format:"number/money",range:"(0,)"},date:{format:"date"},datetime:{format:"date/datetime"},phoneCode:{format:"number",length:"[6]"},imageCode:{length:"[4]"},required:{},cellphone:!0,email:!0,idcard:{pre:"upper,flush",format:"idcard/18:[18,]"},image:"jpg|jpeg|gif|png|bmp"}),(b||(b="undefined"!=typeof window?window:this))&&(b.V=d),void 0)}(jQuery,window); \ No newline at end of file