-
Notifications
You must be signed in to change notification settings - Fork 3
/
jquery-validator.min.js
10 lines (10 loc) · 14.2 KB
/
jquery-validator.min.js
1
2
3
4
5
6
7
8
9
10
/**************************************
@Name: jQuery-Validator 基于jQuery的前端验证框架
@Version: 1.4.0
@Author: Ready
@Date: 2022-01-12
@Documentation: https://codeplayer.vip/p/j7sud
@Email: [email protected]
@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.warn("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;a<arguments.length;a++)c.log(arguments[a]);return!0}},g=d.fn=d.prototype={version:"1.4.0",constructor:d,getValue:function(a){return a.val()},skip:0,filter:function(a){var c=this.skip;return c>0&&(1===(1&c)&&!a.length||2===(2&c)&&!a.is(":visible")||4===(4&c)&&a.is("[readonly]"))?!1:void 0},labelAttr:"label",$:function(b,c){if(!b)return null;if(b instanceof a)return b;if("string"===a.type(b)){var d=b.charAt(0);"#"!=d&&("$"==d&&(b=b.substr(1)),b="[name='"+b+"']")}return a(b,c)},defaultRules:{required:!0},extendRule:function(b,c,d){for(var g,e=b?a.extend({},c):c,f=c.extend;f;){if(g=this.getRule(f),!g)throw"extended rule not found:"+f;for(i in g)i in e||(e[i]=g[i]);f=g.extend}if(d)for(i in d)i in e||(e[i]=d[i]);return e.extend&&delete e.extend,e},clipRule:function(a){return this.extendRule(!0,a,this.defaultRules)},afterHandler:function(b,c){var d=c.rule&&c.rule.after;return d&&a.isFunction(d)&&d.call(this,b,c),b},isRequired:function(a,b){var c=b.$dom.attr("optional");return"true"==c||"false"==c?"true"==c:void 0},pre:{trim:function(b){return a.trim(b)},trimAll:function(a){return e.__whiteSpace||(e.__whiteSpace=/\s/g),a?a.replace(e.__whiteSpace,""):a},lower:function(a){return a?a.toLowerCase():a},upper:function(a){return a?a.toUpperCase():a},flush:function(a,b){var c=b.$dom;return c&&1==c.length&&c.val(a),a}},formatter:{number:function(a,b,c){var h,i,j,k,l,m,g=e[c.rule.format];if(!g){switch(b||(b="+int"),h=b.charAt(0),i="",h){case"+":break;case"-":i="-";break;case"?":i="-?";break;default:h="+",b=h+b}switch(j=b.substr(1),k="+",l="",j){case"int":case"integer":break;case"money":l="(\\.\\d{1,2})?";break;case"float":case"double":l="(\\.\\d+)?";break;default:m=j.split(".",2),k=m[0],m[1]&&(l=m[1].startsWith("{0,")?"(\\.\\d"+m[1].replace(/^\{0,/,"{1,")+")?":"\\.\\d"+m[1])}e[c.rule.format]=g=new RegExp("^"+i+"\\d"+k+l+"$")}return d.debug&&f("number formatter ["+b+"] regexp:",g),g.test(a)?parseFloat(a):!1},date:function(a,b,c){var h,g,i,j,k,l,m;if(b?"datetime"==b?b="yyyy-MM-dd HH:mm:ss":"time"==b&&(b="HH:mm:ss"):b="yyyy-MM-dd",c.dateFormat=b,!a||a.length!=b.length)return!1;if(g=e[c.rule.format],!g){for(i=e.__date||(e.__date=new RegExp("yyyy|MM|dd|HH|mm|ss","g")),g={},i.lastIndex=0;h=i.exec(b);)g[h[0]]=[h.index,h.index+h[0].length];e[c.rule.format]=g}d.debug&&f("date pattern:",g),j={},k=a.split("");for(l in g){for(h=g[l],m=h[0];m<h[1];m++)k[m]=!0;if(h=a.substring(h[0],h[1]),j[l]=parseInt(h,10),j[l]!=h)return!1}for(l in k)if(k[l]!==!0&&k[l]!==b[l])return!1;return j.MM&&j.MM--,new Date(j.yyyy,j.MM||0,j.dd||1,j.HH||0,j.mm||0,j.ss||0)},idcard:function(a,b,c){var h,i,j,k,l,m,n,o,p,q,r,s,g=e[c.rule.format];if(g||(b?(g=b.split(":",2),1==g.length&&g[0].length>2&&(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;k<i.length;k++)j+=a.charAt(k)*i[k];if(isNaN(j)||a.charAt(17)!="10X98765432".charAt(j%11))return!1}if(l=6,m=a.substring(l,l+=h?4:2),n=a.substring(l,l+=2),o=a.substring(l,l+=2),h||(m="19"+m),n--,p=new Date(m,n,o),isNaN(p.getTime()))return!1;if(g[1]){if(q=new Date,p.getTime()>q.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<e.max||"<="==e.right&&e.max==a,!g))return this.sendError(e.right,a,e.max,c),g}},equalsTo:function(a,b,c){var e=this.$(b),f=this.getValue(e,c);return a!=f?(d.util.pushDomContext(null,e,c),this.sendError("",a,f,c),!1):void 0},compare:function(b,c,g){var k,h={},i=g.rule,j=this,l=!0,m=c.replace(e.__compare||(e.__compare=/\b(this|now)\b|([$#])([^\s=+*\/%&|\-]+)/g),function(c,e,f,m){var o,p,q,r,n=e||m;return l&&("this"==n?null==h[n]&&(h[n]=b):"now"==n?null==h[n]&&(h[n]=new Date):(o="#"==f?"#"+n:"[name='"+n+"']",p=a(o),q=j.getValue(p,g),null!=q&&(i.propagation&&(r=a.extend({},g.origin),delete r.compare,j.validate(p,r)===!1&&(l=!1)),d.util.pushDomContext(n,p,g),l&&(i.format?(k=k||d.util.parseFormat(i.format),q=j.formatter[k[0]].call(j,q,k[1],g),q!==!1&&(h[n]=q)):h[n]=q)))),'_hook_["'+n+'"]'});return l?(d.debug&&f("compare validator pattern:"+m,"compare validator hook:",h),k=new Function("_hook_","return ("+m+")").call(null,h),k===!1?(this.sendError("",b,c,g),!1):void 0):!1},cellphone:function(a,b,c){return e.__cellhone||(e.__cellhone=/^1\d{10}$/),d.debug&&f("cellphone regexp:",e.__cellhone),e.__cellhone.test(a)?void 0:(this.sendError("",a,e.__cellhone,c),!1)},email:function(a,b,c){return e.__email||(e.__email=/^[\w._\-]+@[a-zA-Z0-9\-]+(\.[a-zA-Z0-9\-]+)+$/),d.debug&&f("email regexp:",e.__email),e.__email.test(a)?void 0:(this.sendError("",a,e.__email,c),!1)},file:function(a,b,c){var g=e[b]||(e[b]=new RegExp("\\.("+b+")$","i"));return d.debug&&f("file regexp:",g),g.test(a)?void 0:(this.sendError("",a,b,c),!1)},remote:function(b,c,e){for(var i,j,k,g=a.type(c),h={type:"post",async:!1,dataType:"json"};i="function"==g;)c=c.call(null,b,e),g=a.type(c);switch(g){case"string":h.url=c,!i&&e.$dom&&(h.data=e.$dom.serialize());break;case"object":a.extend(h,c);break;default:throw"Unexpected remote validator value:"+c}return j=null,k=h.success,h.success=function(b,c,d){j=b,k&&a.isFunction(k)&&k(b,c,d)},d.debug&&f("remote ajax options:",h),a.ajax(h),(e.remoteResult=j)?(j.callback&&window[j.callback].call(e.$dom,j,b,e),k="OK"==j.status,j.message&&(e.message=j.message),k||this.sendError("",b,c,e),k):!1}},rules:{},define:function(a,b,c){var e,f,h;if(c||(c=!(this instanceof d)),"object"!=typeof a)"object"!=typeof b&&(f={},f[a]=b,b=f),b=this.extendRule(!1,b),h=c?g.rules:this.rules,c||h!==g.rules||(this.rules=h={}),h[a]=b;else for(e in a)g.define.call(this,e,a[e],c)},getRule:function(a){return this.rules&&this.rules[a]||g.rules[a]},validate:function(b,c,e){var h,j,l,m,n,o,p,q,g=this,i=b instanceof a,k=i?b:null;if("string"==typeof c&&(j=c,"?"==j.slice(-1)&&(j=j.slice(0,-1),m=!1),c=g.getRule(j)),h=d.context={ruleName:j,origin:c,$dom:k,rule:c},d.debug&&f("current validate context ["+(i?k.selector:b)+"]:",h),i){if(g.filter(k,h)===!1)return g.afterHandler(!0,h);b=g.getValue(k,h),null==m&&(n=g.isRequired(b,h),"boolean"==typeof n&&(m=n))}if(!c)throw"validate rule not found:"+j;if(c=h.rule=g.clipRule(c),null!=m&&(c.required=m),h.value=b,l={},c.before){if(c.before.call(g,b,h)===!1)return g.afterHandler(!0,h);l.before=!0}if(e&&(h.event=e),c.pre){o=g.pre.trimAll(c.pre).split(",");for(p in o)b=g.pre[o[p]].call(g,b,h);h.value=b,l.pre=!0}if(null==b&&(h.value=b=""),null!=c.required){if(h.validator="required",h.expression=c.required,g.validator.required.call(g,b,c.required,h)===!1)return g.afterHandler(!1,h);if(h._stop)return g.afterHandler(!0,h);l.required=!0}if(c.format){if(h.validator="format",h.expression=c.format,b=g.validator.format.call(g,b,c.format,h),b===!1)return g.afterHandler(!1,h);if(h._stop)return g.afterHandler(!0,h);l.format=!0}for(p in c)if(q=g.validator[p],q&&!l[p]&&(l[p]=!0)){if(h.validator=p,h.expression=c[p],q.call(g,b,c[p],h)===!1)return g.afterHandler(!1,h);if(h._stop)return g.afterHandler(!0,h)}return g.afterHandler(!0,h)},execute:function(b,c,d){var f,e=this;return null==d&&(d=c,c=null),b instanceof a||(b=a(b)),b.length?"string"==typeof c?(b.bind(c,function(a){var g,b="submit"==a.type,c=b?this:a.target,f=d;return b||(null==(g=c.id)||null==(f=d["#"+g]))&&null==(f=d[g=c.name])&&(f=d["$"+g]),f?e.execute(c,a,f):!1}),void 0):(f=!0,b.each(function(){var h,b=a(this),g=this.nodeName;if("FORM"==g||"form"==g){for(h in d)if(e.validate(e.$(h,b),d[h],c)===!1)return f=!1}else if(e.validate(b,d,c)===!1)return f=!1}),e.callback&&a.isFunction(e.callback)?e.callback.call(b,f,d,c)!==!1:f):!this.strict},bindAttr:function(b){var c=a.extend({container:"form",eventType:"submit",attr:"v",nameAsValue:!0,cache:!0},b),e=this,g=a(c.container),h=g.find("["+c.attr+"]"),i=function(b,i){var j=b?a(b):c.cache?h:g.find("["+c.attr+"]"),k=!0,l={};return j.each(function(){var b=a(this),h=this.name||b.attr("name"),j=b.attr(c.attr)||c.nameAsValue&&h;if(d.debug&&f("current element ["+j+"] :",b),!j)throw"invalid attribute ["+c.attr+"]:"+b;return l[h]?void 0:(null==this.checked||"radio"!=this.type&&"checkbox"!=this.type||(b=g.find("[name='"+h+"']")),l[h]=j,e.validate(b,j,i)===!1?k=!1:void 0)}),e.callback&&a.isFunction(e.callback)?e.callback.call(j,k,l,i)!==!1:k};return c.eventType&&g.bind(c.eventType,function(a){return i(null,a)}),{validate:i}},sendError:function(b,c,d,e){var f,g;e.trigger=b||e.validator,e.actual=c,e.expected=d,e.label=e.label||this.getLabel(e.name,e.$dom,e),f=this.getMessage(e),f!==!1&&(g=a.isFunction(e.rule.renderError)?e.rule.renderError:this.renderError,g.call(this,f,this.$(e.rule.errorFocus)||e.$dom,e))},renderError:function(b,c,d){c&&c.length&&a.isFunction(c.tips)&&c.is(":visible")?c.tips(b):a.isFunction(a.alert)?a.alert(b):alert(b);var e=d.event;(!e||"focusout"!=e.type&&"blur"!=e.type)&&c&&c.first().focus()},messages:{required:"{label}不能为空!","required.checked":"请先选择{label}!",equalsTo:"{label}必须与{#0}输入一致!","==":"{label}必须等于{expected}!",">=":"{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")||this.findLabel&&this.findLabel(a,b,c)||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);