forked from rickharrison/validate.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate.min.js
1 lines (1 loc) · 7.48 KB
/
validate.min.js
1
!function(e,t,a){var n={messages:{required:"字段 %s 是必填项。",matches:"字段 %s 和字段 %s 不一致。","default":"字段 %s 仍然是默认值,请修改",valid_email:"字段 %s 必须包含合法Email地址。",valid_emails:"字段 %s 必须包含全部合法Email地址。",min_length:"字段 %s 长度上最少包含 %s 个字符。",max_length:"字段 %s 长度上不能超过 %s 个字符。",exact_length:"字段 %s 长度上必须是 %s 个字符。",greater_than:"字段 %s 必须包含的数字必须大于 %s。",less_than:"字段 %s 必须包含的数字必须小于 %s。",alpha:"字段 %s 只能包含字母。",alpha_numeric:"字段 %s 只能包含字母和数字。",alpha_dash:"字段 %s 只能包含字母和数字,下划线(_)和斜线(/)。",numeric:"字段 %s 只能包含数字。",integer:"字段 %s 必须是整数。",decimal:"字段 %s 必须是小数。",is_natural:"字段 %s 只能包含正数。",is_natural_no_zero:"字段 %s 的数字必须大于0。",valid_ip:"字段 %s 必须包含合法的IP地址。",valid_base64:"字段 %s 必须是一个 base64 字符串。",valid_credit_card:"字段 %s 包含一个有效的信用卡号码。",is_file_type:"字段 %s 只能包含 %s 类型的文件。",valid_url:"字段 %s 只能包含有效的URL地址。",greater_than_date:"字段 %s 必须是一个晚于 %s的日期。",less_than_date:"字段 %s 必须是一个早于 %s的日期。",greater_than_or_equal_date:"字段 %s 必须是一个晚于或等于 %s的日期。",less_than_or_equal_date:"字段 %s 必须是一个早于或等于 %s 的日期。"},callback:function(e){}},s=/^(.+?)\[(.+)\]$/,r=/^[0-9]+$/,i=/^\-?[0-9]+$/,l=/^\-?[0-9]*\.?[0-9]+$/,u=/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/,o=/^[a-z]+$/i,d=/^[a-z0-9]+$/i,h=/^[a-z0-9_\-]+$/i,c=/^[0-9]+$/i,f=/^[1-9][0-9]*$/i,p=/^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})$/i,_=/[^a-zA-Z0-9\/\+=]/i,v=/^[\d\-\s]+$/,m=/^((http|https):\/\/(\w+:{0,1}\w*@)?(\S+)|)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$/,g=/\d{4}-\d{1,2}-\d{1,2}/,y=function(e,t,s){this.callback=s||n.callback,this.errors=[],this.fields={},this.form=this._formByNameOrNode(e)||{},this.messages={},this.handlers={},this.conditionals={};for(var r=0,i=t.length;i>r;r++){var l=t[r];if((l.name||l.names)&&l.rules)if(l.names)for(var u=0,o=l.names.length;o>u;u++)this._addField(l,l.names[u]);else this._addField(l,l.name);else console.warn("validate.js: The following field is being skipped due to a misconfiguration:"),console.warn(l),console.warn("Check to ensure you have properly configured a name and rules for this field")}var d=this.form.onsubmit;this.form.onsubmit=function(e){return function(t){try{return e._validateForm(t)&&(d===a||d())}catch(n){}}}(this)},b=function(e,t){var a;{if(!(e.length>0)||"radio"!==e[0].type&&"checkbox"!==e[0].type)return e[t];for(a=0,elementLength=e.length;a<elementLength;a++)if(e[a].checked)return e[a][t]}};y.prototype.setMessage=function(e,t){return this.messages[e]=t,this},y.prototype.registerCallback=function(e,t){return e&&"string"==typeof e&&t&&"function"==typeof t&&(this.handlers[e]=t),this},y.prototype.registerConditional=function(e,t){return e&&"string"==typeof e&&t&&"function"==typeof t&&(this.conditionals[e]=t),this},y.prototype._formByNameOrNode=function(e){return"object"==typeof e?e:t.forms[e]},y.prototype._addField=function(e,t){this.fields[t]={name:t,display:e.display||t,rules:e.rules,depends:e.depends,id:null,element:null,type:null,value:null,checked:null}},y.prototype._validateForm=function(e){this.errors=[];for(var t in this.fields)if(this.fields.hasOwnProperty(t)){var n=this.fields[t]||{},s=this.form[n.name];s&&s!==a&&(n.id=b(s,"id"),n.element=s,n.type=s.length>0?s[0].type:s.type,n.value=b(s,"value"),n.checked=b(s,"checked"),n.depends&&"function"==typeof n.depends?n.depends.call(this,n)&&this._validateField(n):n.depends&&"string"==typeof n.depends&&this.conditionals[n.depends]?this.conditionals[n.depends].call(this,n)&&this._validateField(n):this._validateField(n))}return"function"==typeof this.callback&&this.callback(this.errors,e),this.errors.length>0&&(e&&e.preventDefault?e.preventDefault():event&&(event.returnValue=!1)),!0},y.prototype._validateField=function(e){for(var t=e.rules.split("|"),r=e.rules.indexOf("required"),i=!e.value||""===e.value||e.value===a,l=0,u=t.length;u>l;l++){var o=t[l],d=null,h=!1,c=s.exec(o);if((-1!==r||-1!==o.indexOf("!callback_")||!i)&&(c&&(o=c[1],d=c[2]),"!"===o.charAt(0)&&(o=o.substring(1,o.length)),"function"==typeof this._hooks[o]?this._hooks[o].apply(this,[e,d])||(h=!0):"callback_"===o.substring(0,9)&&(o=o.substring(9,o.length),"function"==typeof this.handlers[o]&&this.handlers[o].apply(this,[e.value,d,e])===!1&&(h=!0)),h)){var f=this.messages[e.name+"."+o]||this.messages[o]||n.messages[o],p="An error has occurred with the "+e.display+" field.";f&&(p=f.replace("%s",e.display),d&&(p=p.replace("%s",this.fields[d]?this.fields[d].display:d))),this.errors.push({id:e.id,element:e.element,name:e.name,message:p,rule:o});break}}},y.prototype._getValidDate=function(e){if(!e.match("today")&&!e.match(g))return!1;var t,a=new Date;return e.match("today")||(t=e.split("-"),a.setFullYear(t[0]),a.setMonth(t[1]-1),a.setDate(t[2])),a},y.prototype._hooks={required:function(e){var t=e.value;return"checkbox"===e.type||"radio"===e.type?e.checked===!0:null!==t&&""!==t},"default":function(e,t){return e.value!==t},matches:function(e,t){var a=this.form[t];return a?e.value===a.value:!1},valid_email:function(e){return u.test(e.value)},valid_emails:function(e){for(var t=e.value.split(/\s*,\s*/g),a=0,n=t.length;n>a;a++)if(!u.test(t[a]))return!1;return!0},min_length:function(e,t){return r.test(t)?e.value.length>=parseInt(t,10):!1},max_length:function(e,t){return r.test(t)?e.value.length<=parseInt(t,10):!1},exact_length:function(e,t){return r.test(t)?e.value.length===parseInt(t,10):!1},greater_than:function(e,t){return l.test(e.value)?parseFloat(e.value)>parseFloat(t):!1},less_than:function(e,t){return l.test(e.value)?parseFloat(e.value)<parseFloat(t):!1},alpha:function(e){return o.test(e.value)},alpha_numeric:function(e){return d.test(e.value)},alpha_dash:function(e){return h.test(e.value)},numeric:function(e){return r.test(e.value)},integer:function(e){return i.test(e.value)},decimal:function(e){return l.test(e.value)},is_natural:function(e){return c.test(e.value)},is_natural_no_zero:function(e){return f.test(e.value)},valid_ip:function(e){return p.test(e.value)},valid_base64:function(e){return _.test(e.value)},valid_url:function(e){return m.test(e.value)},valid_credit_card:function(e){if(!v.test(e.value))return!1;for(var t=0,a=0,n=!1,s=e.value.replace(/\D/g,""),r=s.length-1;r>=0;r--){var i=s.charAt(r);a=parseInt(i,10),n&&(a*=2)>9&&(a-=9),t+=a,n=!n}return t%10===0},is_file_type:function(e,t){if("file"!==e.type)return!0;var a=e.value.substr(e.value.lastIndexOf(".")+1),n=t.split(","),s=!1,r=0,i=n.length;for(r;i>r;r++)a==n[r]&&(s=!0);return s},greater_than_date:function(e,t){var a=this._getValidDate(e.value),n=this._getValidDate(t);return n&&a?a>n:!1},less_than_date:function(e,t){var a=this._getValidDate(e.value),n=this._getValidDate(t);return n&&a?n>a:!1},greater_than_or_equal_date:function(e,t){var a=this._getValidDate(e.value),n=this._getValidDate(t);return n&&a?a>=n:!1},less_than_or_equal_date:function(e,t){var a=this._getValidDate(e.value),n=this._getValidDate(t);return n&&a?n>=a:!1}},e.FormValidator=y}(window,document),"undefined"!=typeof module&&module.exports&&(module.exports=FormValidator);