-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate.js
17 lines (16 loc) · 4.65 KB
/
validate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
* validate.js 1.1
* Copyright (c) 2012 Rick Harrison, http://rickharrison.me
* validate.js is open sourced under the MIT license.
* Portions of validate.js are inspired by CodeIgniter.
* http://rickharrison.github.com/validate.js
*/
(function(k,l,i){var m={required:"The %s field is required.",matches:"The %s field does not match the %s field.",valid_email:"The %s field must contain a valid email address.",valid_emails:"The %s field must contain all valid email addresses.",min_length:"The %s field must be at least %s characters in length.",max_length:"The %s field must not exceed %s characters in length.",exact_length:"The %s field must be exactly %s characters in length.",greater_than:"The %s field must contain a number greater than %s.",
less_than:"The %s field must contain a number less than %s.",alpha:"The %s field must only contain alphabetical characters.",alpha_numeric:"The %s field must only contain alpha-numeric characters.",alpha_dash:"The %s field must only contain alpha-numeric characters, underscores, and dashes.",numeric:"The %s field must contain only numbers.",integer:"The %s field must contain an integer.",decimal:"The %s field must contain a decimal number.",is_natural:"The %s field must contain only positive numbers.",
is_natural_no_zero:"The %s field must contain a number greater than zero.",valid_ip:"The %s field must contain a valid IP.",valid_base64:"The %s field must contain a base64 string."},n=function(){},o=/^(.+)\[(.+)\]$/,h=/^[0-9]+$/,p=/^\-?[0-9]+$/,g=/^\-?[0-9]*\.?[0-9]+$/,j=/^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,6}$/i,q=/^[a-z]+$/i,r=/^[a-z0-9]+$/i,s=/^[a-z0-9_-]+$/i,t=/^[0-9]+$/i,u=/^[1-9][0-9]*$/i,v=/^((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,w=
/[^a-zA-Z0-9\/\+=]/i,e=function(a,b,c){this.callback=c||n;this.errors=[];this.fields={};this.form=l.forms[a]||{};this.messages={};this.handlers={};a=0;for(c=b.length;a<c;a++){var d=b[a];d.name&&d.rules&&(this.fields[d.name]={name:d.name,display:d.display||d.name,rules:d.rules,id:null,type:null,value:null,checked:null})}this.form.onsubmit=function(a){return function(b){try{return a._validateForm(b)}catch(c){}}}(this)};e.prototype.setMessage=function(a,b){this.messages[a]=b;return this};e.prototype.registerCallback=
function(a,b){a&&("string"===typeof a&&b&&"function"===typeof b)&&(this.handlers[a]=b);return this};e.prototype._validateForm=function(a){this.errors=[];for(var b in this.fields)if(this.fields.hasOwnProperty(b)){var c=this.fields[b]||{},d=this.form[c.name];d&&d!==i&&(c.id=d.id,c.type=d.type,c.value=d.value,c.checked=d.checked);this._validateField(c)}"function"===typeof this.callback&&this.callback(this.errors,a);if(0<this.errors.length)if(a&&a.preventDefault)a.preventDefault();else return!1;return!0};
e.prototype._validateField=function(a){var b=a.rules.split("|");if(!(-1===a.rules.indexOf("required")&&(!a.value||""===a.value||a.value===i)))for(var c=0,d=b.length;c<d;c++){var f=b[c],e=null,g=!1;if(parts=o.exec(f))f=parts[1],e=parts[2];"function"===typeof this._hooks[f]?this._hooks[f].apply(this,[a,e])||(g=!0):"callback_"===f.substring(0,9)&&(f=f.substring(9,f.length),"function"===typeof this.handlers[f]&&!1===this.handlers[f].apply(this,[a.value])&&(g=!0));if(g){b=this.messages[f]||m[f];c="An error has occurred with the "+
a.display+" field.";b&&(c=b.replace("%s",a.display),e&&(c=c.replace("%s",this.fields[e]?this.fields[e].display:e)));this.errors.push({id:a.id,name:a.name,message:c});break}}};e.prototype._hooks={required:function(a){var b=a.value;return"checkbox"===a.type?!0===a.checked:null!==b&&""!==b},matches:function(a,b){return(el=this.form[b])?a.value===el.value:!1},valid_email:function(a){return j.test(a.value)},valid_emails:function(a){for(var a=a.value.split(","),b=0;b<a.length;b++)if(!j.test(a[b]))return!1;
return!0},min_length:function(a,b){return!h.test(b)?!1:a.value.length>=parseInt(b,10)},max_length:function(a,b){return!h.test(b)?!1:a.value.length<=parseInt(b,10)},exact_length:function(a,b){return!h.test(b)?!1:a.value.length===parseInt(b,10)},greater_than:function(a,b){return!g.test(a.value)?!1:parseFloat(a.value)>parseFloat(b)},less_than:function(a,b){return!g.test(a.value)?!1:parseFloat(a.value)<parseFloat(b)},alpha:function(a){return q.test(a.value)},alpha_numeric:function(a){return r.test(a.value)},
alpha_dash:function(a){return s.test(a.value)},numeric:function(a){return g.test(a.value)},integer:function(a){return p.test(a.value)},decimal:function(a){return g.test(a.value)},is_natural:function(a){return t.test(a.value)},is_natural_no_zero:function(a){return u.test(a.value)},valid_ip:function(a){return v.test(a.value)},valid_base64:function(a){return w.test(a.value)}};k.FormValidator=e})(window,document);