thingspeak/app/assets/javascripts/validate.min.js
2014-02-17 12:05:39 -05:00

17 lines
3.8 KiB
JavaScript

/*
* validate.js 1.0.1
* Copyright (c) 2011 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(j,k,i){var l={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.",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."},m=function(){},n=/^(.+)\[(.+)\]$/,g=/^[0-9]+$/,o=/^\-?[0-9]+$/,h=/^\-?[0-9]*\.?[0-9]+$/,p=/^[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,
e=function(a,b,d){this.callback=d||m;this.errors=[];this.fields={};this.form=k.forms[a]||{};this.messages={};this.handlers={};a=0;for(d=b.length;a<d;a++){var c=b[a];c.name&&c.rules&&(this.fields[c.name]={name:c.name,display:c.display||c.name,rules:c.rules,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&&typeof a==="string"&&b&&typeof b==="function"&&(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 d=this.fields[b]||{},c=this.form[d.name];if(c&&c!==i)d.type=c.type,d.value=c.value,d.checked=c.checked;this._validateField(d)}typeof this.callback==="function"&&this.callback(this.errors,a);if(this.errors.length>0)if(a&&a.preventDefault)a.preventDefault();else return false;return true};e.prototype._validateField=
function(a){var b=a.rules.split("|");if(!(a.rules.indexOf("required")===-1&&(!a.value||a.value===""||a.value===i)))for(var d=0,c=b.length;d<c;d++){var f=b[d],e=null,g=false;if(parts=n.exec(f))f=parts[1],e=parts[2];typeof this._hooks[f]==="function"?this._hooks[f].apply(this,[a,e])||(g=true):f.substring(0,9)==="callback_"&&(f=f.substring(9,f.length),typeof this.handlers[f]==="function"&&this.handlers[f].apply(this,[a.value])===false&&(g=true));if(g){(b=this.messages[f]||l[f])?(a=b.replace("%s",a.display),
e&&(a=a.replace("%s",this.fields[e]?this.fields[e].display:e)),this.errors.push(a)):this.errors.push("An error has occurred with the "+a.display+" field.");break}}};e.prototype._hooks={required:function(a){var b=a.value;return a.type==="checkbox"?a.checked===true:b!==null&&b!==""},matches:function(a,b){return(el=this.form[b])?a.value===el.value:false},valid_email:function(a){return p.test(a.value)},min_length:function(a,b){return!g.test(b)?false:a.value.length>=b},max_length:function(a,b){return!g.test(b)?
false:a.value.length<=b},exact_length:function(a,b){return!g.test(b)?false:a.value.length==b},greater_than:function(a,b){return!h.test(a.value)?false:parseFloat(a.value)>parseFloat(b)},less_than:function(a,b){return!h.test(a.value)?false: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 h.test(a.value)},integer:function(a){return o.test(a.value)}};
j.FormValidator=e})(window,document);