-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyavp.min.js
9 lines (8 loc) · 8.82 KB
/
yavp.min.js
1
2
3
4
5
6
7
8
9
/*!
* Yet Another Validation Plugin v1.0.0
* https://github.com/mateuszmarkowski/yavp
*
* Copyright 2013, 2014 Mateusz Markowski
* Released under the MIT license
*/
(function(e){function t(){var n=arguments,r={success:t.success||null,error:t.error||null,elementSuccess:t.elementSuccess||null,elementError:t.elementError||null,before:t.before||null,after:t.after||null,elementBefore:t.elementBefore||null,elementAfter:t.elementAfter||null,triggers:t.trigers||["change","keydown"],debounce:typeof t.debounce==="undefined"?350:t.debounce,validators:t.validators||{},messages:t.messages||{},collections:t.collections||{},selectorOverwrite:typeof t.selectorOverwrite==="undefined"?false:t.selectorOverwrite,fields:{}},i,s,o,u,a,f,l=[];if(this.data("yavp")&&typeof n[0]==="string"){var c=this.data("yavp")[n[0]].call(this);return typeof c==="undefined"?this:c}else if(this.data("yavp")){return this.data("yavp")}else if(typeof n[0]==="string"&&n[0]==="validates"&&this.data("yavp.parent")){return this.data("yavp.parent").data("yavp").validates.call(this)}this.each(function(){function t(){function u(e){return e in r.validators?r.validators[e]:false}var t,n=l.length>0?true:false;l=[];i=e();e.each(r.fields,function(a,c){var h={},p={};if(typeof c==="string"){t=c.split(".");if(t.length!==2){return}h=r.collections[t[0]][t[1]];if(Array.isArray(h)){h={selector:'[name="'+t[1]+'"]',validators:h}}}else if(typeof c==="object"){h=c}if(r.selectorOverwrite!==true&&h.selectorOverwrite!==true||r.selectorOverwrite===true&&h.selectorOverwrite===false){h.selector+=":visible:enabled"}p=s.find(h.selector);if(p.length===0){return}if(n||typeof p.data("yavp.validators")==="undefined"){p.data("yavp.validators",[])}if(n||typeof p.data("yavp.params")==="undefined"){p.data("yavp.params",{})}if(typeof h.chain!=="undefined"){p.data("yavp.chain",h.chain)}p.data("yavp.parent",s);e.each(h.validators,function(t,n){if(Object.prototype.toString.call(n)==="[object RegExp]"){p.data("yavp.validators").push({name:"regexp",callback:r.validators.regexp});p.data("yavp.params").regexp={regexp:n}}else if(typeof n==="string"){if(callback=u(n)){p.data("yavp.validators").push({name:n,callback:callback})}}else if(typeof n==="object"){if(callback=u(n.validator)){p.data("yavp.validators").push({name:n.validator,callback:callback});p.data("yavp.params")[n.validator]=function(){var t={};e.each(n,function(e,n){if(e!=="validator"){t[e]=n}});return t}()}}else if(typeof n==="function"){p.data("yavp.validators").push({name:"anonymous-validator-"+ ++f,callback:n})}});p.data("yavp.messages",h.messages||{});if(typeof h.elementSuccess==="function"){p.data("elementSuccess",h.elementSuccess)}if(typeof h.elementError==="function"){p.data("elementError",h.elementError)}if(r.cache!==false||typeof h.cache==="undefined"||typeof h.cache!=="undefined"&&h.cache!==false){p.each(function(){o=e(this);if(o.prop("tagName").toLowerCase()==="input"&&e.inArray(o.attr("type").toLowerCase(),["checkbox","radio"])===-1||o.prop("tagName").toLowerCase()==="textarea"){p.data("yavp.cacheSuccess",[]);p.data("yavp.cacheError",{})}})}i=i.add(p);l.push(h.selector)})}function c(){var t=function(t){e.each(t,function(e,n){t[e]=n+".yavp"});return t.join(" ")}(r.triggers);s.off(t);s.on(t,l.join(","),r.debounce?h(d,r.debounce):d)}function h(e,t){var n,r=0;return function(){var i=this,s=(new Date).getTime(),o=arguments;if(n&&s-t<r){clearTimeout(n);n=null}if(!n){n=setTimeout(function(){e.apply(i,Array.prototype.slice.call(o));n=null},t)}r=s}}function p(t){function l(){var t=this;if(a===false&&typeof t.data("yavp.cacheSuccess")==="object"){if(e.inArray(t.val(),t.data("yavp.cacheSuccess"))===-1){t.data("yavp.cacheSuccess").push(t.val())}}if(t.data("elementSuccess")){t.data("elementSuccess").call(t)}else if(r.elementSuccess){r.elementSuccess.call(t)}}function c(e,t){var n,s=this;i=true;if(e&&t){n=e;t=t}else if(this.data("yavp.messages")&&e in s.data("yavp.messages")){n=e;t=s.data("yavp.messages")[e]}else if(e in r.messages){n=e;t=r.messages[e]}else if(/^anonymous-validator-[0-9]+$/.test(e)){n="format";t=s.data("yavp.messages")[n]||r.messages[n]}else if(e in r.validators){n="format";t=r.messages[n]}else{n="format";t=r.messages[n]}if(typeof t=="function"){t=t.call(s,s.data("yavp.params")[n]||{})}if(a===false&&typeof s.data("yavp.cacheError")==="object"){s.data("yavp.cacheError")[s.val()]={type:n,message:t}}u.push({element:s,type:n,message:t});if(s.data("elementError")){s.data("elementError").call(s,t,n)}else if(r.elementError){r.elementError.call(s,t,n)}}function h(e,t,n){return{status:"active",revoke:function(){this.status="inactive";f.reject();return this},dontCache:function(){a=true},success:function(e){var e=typeof e==="undefined"?false:e;if(this.status==="inactive"){return this}this.status="inactive";if(e){f.resolve()}else{t.resolve()}return this},error:function(t){if(this.status==="inactive"){return this}this.status="inactive";c.call(e,n,t);f.reject();return this}}}var n,i=false,s=e(t),o=s.data("yavp.validators")?s.data("yavp.validators").slice():[],a=false,f=e.Deferred();if(s.data("yavp.asyncResultInstance")&&s.data("yavp.asyncResultInstance").status==="active"){s.data("yavp.asyncResultInstance").revoke()}if(r.elementBefore){r.elementBefore.call(s)}f.done(function(){!i&&l.call(s)}).fail(function(){}).always(function(){if(r.elementAfter){r.elementAfter.call(s)}});if(typeof s.data("yavp.cacheError")=="object"&&s.val()in s.data("yavp.cacheError")){var p=s.data("yavp.cacheError")[s.val()];(new h(s,f,p.type)).error(p.message);return f.promise()}if(typeof s.data("yavp.cacheSuccess")=="object"&&e.inArray(s.val(),s.data("yavp.cacheSuccess"))>-1){(new h(s,f)).success();return f.promise()}(function d(t){if(!t.length){f.resolve();return}var r,i,o=e.Deferred();o.done(function(){d(t)}).fail(function(){});r=t.shift();n=h(s,o,r.name);s.data("yavp.asyncResultInstance",n);i=r.callback.call(s,n,s.data("yavp.params")[r.name]||{});if(typeof i==="boolean"){i===true?n.success():n.error()}})(o);return f.promise()}function d(){var t=[],n=e.Deferred(),o=this instanceof e?this:e(this),a;u=[];if(typeof r.before=="function"){r.before.call(s)}if(typeof r.after=="function"){n.always(function(){r.after.call(s)})}if(o.prop("tagName").toLowerCase()==="form"){a=i}else{a=o;if(typeof o.data("yavp.chain")!=="undefined"){a=a.add(s.find(o.data("yavp.chain")))}}a.each(function(e,n){t.push(p(n))});if(t.length){e.when.apply(null,t).done(function(){n.resolve()}).fail(function(){n.reject()})}else{n.resolve()}return n.promise()}s=e(this);u=[];a=false;f=100;if(typeof n[0]==="function"){r=e.extend(true,{},r,{success:n[0]})}else{r=e.extend(true,{},r,n[0])}s.attr("novalidate",true);s.submit(function(t){var n=this,i=e.Event("submit",{target:n});t.preventDefault();e.when(d.call(n)).done(function(){a=typeof r.success=="function"?r.success.call(n,i)!==false:true;if(a&&!i.isDefaultPrevented()){n.submit()}}).fail(function(){if(typeof r.error=="function"){r.error.call(n,u)}})});t();c();s.data("yavp",{refresh:function(){t();c()},validates:function(){return d.call(this)}})});return this}e.fn.yavp=t;t.validators={optional:function(e){if(this.val().length===0){e.success(true)}else{return true}},required:function(){return this.val().length===0?false:true},number:function(){return/^[0-9]+$/.test(this.val())},decimal:function(){return/^[0-9]+(\.[0-9]+)?$/.test(this.val())},name:function(){return/^[a-z ']+$/i.test(this.val())},email:function(){return/^[-0-9a-z.+_]+@[-0-9a-z.+_]+\.[a-z]{2,4}$/i.test(this.val())},length:function(e,t){var n=this.val().length,r=parseInt(t.min),i=parseInt(t.max);if(!isNaN(r)&&n<r){return false}if(!isNaN(i)&&n>i){return false}return true},range:function(e,t){var n=parseFloat(this.val()),r=parseFloat(t.min),i=parseFloat(t.max);if(!isNaN(r)&&n<r){return false}if(!isNaN(i)&&n>i){return false}return true},equals:function(e,t){e.dontCache();return this.val()==this.parents("form:first").find(t.selector).val()},regexp:function(e,t){return t.regexp.test(this.val())},checked:function(e){e.dontCache();return this.is(":checked")},year:function(e){var t=this.val();return/^[0-9]{4}$/.test(t)&&t>=1900&&t<=2100},file:function(t,n){var r=Array.isArray(n.extensions)?n.extensions:[n.extensions],i=this.val().split(".");if(i.length===0){return false}else if(e.inArray(i.pop().toLowerCase(),r)>-1){return true}else{return false}}};t.messages={format:"Incorrect format",equals:"Fields don't match",required:"Required field",checked:"Please check this box",range:function(e){if(e.min&&e.max){return"Please pick a number between "+e.min+" and "+e.max}else if(e.min){return"Please pick a number greater than "+e.min}else{return"Please pick a number smaller than "+e.max}},length:function(e){if(e.min&&e.max){return"Enter between "+e.min+" and "+e.max+" characters"}else if(e.min){return"Enter at least "+e.min+" characters"}else{return"Enter at most "+e.max+" characters"}},file:function(e){var t=Array.isArray(e.extensions)?e.extensions:[e.extensions];return"Invalid file extension! Allowed file extensions include: "+t.join(", ")}}})(jQuery)