-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.controle.min.js
5 lines (5 loc) · 2.69 KB
/
jquery.controle.min.js
1
2
3
4
5
/*!
* @author Thomas <thansen@solire.fr>
* @licence CC BY-NC 4.0 http://creativecommons.org/licenses/by-nc/4.0/
*/
!function(a){a.fn.controle=function(b,c){function d(c,d){this.form=a(c),this.elmt=a(d),this.check=function(){var c,d,e,j,k,l,m,n,o;return f(b.beforeElmtCheck)&&(n=b.beforeElmtCheck,n.call(this.form,this.elmt,this)),k=this.elmt.val(),c=h,"undefined"==typeof this.elmt.attr("class")?h:(d=this.elmt.attr("class").split(" "),m=a.inArray("form-controle",d),d.length<m+3?h:(e="form-oblig"==d[m+1]||""!=k&&0!=k,j=d[m+2].replace("form-",""),e&&j in i&&(l=i[j],f(l)?c=l.call(this.form,k,this.elmt,this):(o=a.isArray(l)?l:[l],a.each(o,function(a,b){c&&(c=b.test(k))}))),c===g?(f(b.ifWrong)&&(n=b.ifWrong,n.call(this.form,this.elmt,this)),g):(f(b.ifRight)&&(n=b.ifRight,n.call(this.form,this.elmt,this)),h)))}}function e(c){this.form=a(c),this.champs=[],this.init=function(){var c=this;c.champs=[],a(b.controlHandler,this.form).each(function(){c.champs.push(new d(this.form,this))})},this.check=function(){var c=h;return a.each(this.champs,function(d,e){var h;e.check()||c&&(c=g,f(b.ifFirstWrong)&&(h=b.ifFirstWrong,h.call(a(self),e.elmt,e)))}),c},this.init()}function f(b){return a.isFunction(b)}var g=!1,h=!g,i={num:/^[0-9]+$/,numnotnul:/^[1-9]{1}|[0-9]{2,16}$/,tel:/^[\s\/\(\)\+\.0-9]{8,20}$/,cp:/^[0-9]{4,5}$/,heure:/^[0-9]{2}:[0-9]{2}(:[0-9]|)$/,date:/^[0-9]{2,2}\/[0-9]{2,2}\/[0-9]{4,4}$/,email:/^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,3}$/,url:/^(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$/,rew:/^[0-9a-z-]{1,50}$/,notnul:function(a){return""!=a&&0!=a},mix:function(a){return a.length>2},atleastonechecked:function(b,c){var d=c.attr("name"),e=a('input[name="'+d+'"]:checked',this).length;return e>0}},j={controlHandler:".form-controle",ajax:g,elmtEvents:g,ifFirstWrong:g,ifWrong:g,ifRight:g,beforeElmtCheck:g,beforeCheck:g,beforeSubmit:g,afterSubmit:g};return i=a.extend({},i,c),b=b||{},events=b.elmtEvents||{},events=a.extend({},j.elmtEvents,events),b=a.extend(j,b),b.elmtEvents=events,this.each(function(){var c,d,i=this,j=a(i).attr("method"),k=new e(this);if(("undefined"==typeof j||null==j)&&(j="post"),b.elmtEvents)for(evnt in b.elmtEvents)a.each(k.champs,function(a,c){var d=evnt;c.elmt.bind(d,function(a){b.elmtEvents[d].call(this,a,c)})});k.form.submit(function(e){var i,l;if(k.init(),f(b.beforeCheck)&&(c=b.beforeCheck,c.call(k.form,k)),d=k.check()){if(f(b.beforeSubmit)&&(c=b.beforeSubmit,"undefined"!=typeof c.call(k.form,k)&&c.call(k.form,k)===!1))return!1;if(b.ajax===g)return h;l=k.form.attr("action"),i=k.form.serialize(),a.ajax({url:l,data:i,type:j,dataType:"json",success:function(a){f(b.afterSubmit)&&(c=b.afterSubmit,c.call(k.form,a,k))}})}return e.preventDefault(),g})})}}(jQuery);