You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1 lines
5.7 KiB

(function(e,t,n,o){e.fn.form=function(t,i){var a,r=e(this),s=(e(n),e.extend(!0,{},e.fn.form.settings,i)),c=("."+s.namespace,"module-"+s.namespace,r.selector||""),l=(new Date).getTime(),u=[],d=arguments[0],m="string"==typeof d,f=[].slice.call(arguments,1);return r.each(function(){var n,i=e(this),p=e(this).find(s.selector.group),g=e(this).find(s.selector.field),h=(e(this).find(s.selector.prompt),[]),v=this,b=i.data("module-"+s.namespace),y=s.namespace,x=s.metadata,w=s.className,C=s.errors;n={initialize:function(){n.verbose("Initializing form validation"),t===o&&e.isEmptyObject(t)?n.error(C.noFields,i):"submit"==s.on&&i.on("submit."+y,n.validate.form)},destroy:function(){i.off(y)},refresh:function(){g=i.find(s.selector.field)},field:{find:function(t){return n.refresh(),g.filter("#"+t).size()>0?g.filter("#"+t):g.filter('[name="'+t+'"]').size()>0?g.filter('[name="'+t+'"]'):g.filter("[data-"+x.validate+'="'+t+'"]').size()>0?g.filter("[data-"+x.validate+'="'+t+'"]'):e("<input/>")},add:{error:function(t,o){var i=n.field.find(t.identifier),a=i.closest(p),r=p.find(r),c=0!==r.size();a.addClass(w.error),s.inlineError&&(c||(r=e("<div />").addClass(w.prompt).insertBefore(i)),r.html(o[0]).fadeIn(s.animateSpeed))}},remove:{error:function(e){var t=n.field.find(e.identifier),o=t.closest(p),i=p.find(i);o.removeClass(w.error),s.inlineError&&i.hide()}}},validate:{form:function(o){var i=!0;return h=[],e.each(t,function(e,t){n.validate.field(t)||(i=!1)}),i?e.proxy(s.onSuccess,this)(o):e.proxy(s.onFailure,this)(h)},field:function(t){var i=n.field.find(t.identifier),a=!0,r=[];return t.rules!==o&&e.each(t.rules,function(e,o){n.validate.rule(t,o)||(n.debug("Field is invalid",t.identifier,o.type),r.push(o.prompt),a=!1)}),a?(n.field.remove.error(t,r),s.onValid(i),!0):(h=h.concat(r),n.field.add.error(t,r),e.proxy(s.onInvalid,i)(r),!1)},rule:function(t,a){var r,c,l=n.field.find(t.identifier),u=a.type,d=l.val(),m=/\[(.*?)\]/i,f=m.exec(u),p=!0;return f!==o&&null!=f?(r=f[1],c=u.replace(f[0],""),p=e.proxy(s.rules[c],i)(d,r)):p="checked"==u?l.filter(":checked").size()>0:s.rules[u](d),p}},setting:function(t,n){return n===o?s[t]:(e.isPlainObject(t)?e.extend(!0,s,t):s[t]=n,o)},internal:function(t,i){return i===o?n[t]:(e.isPlainObject(t)?e.extend(!0,n,t):n[t]=i,o)},debug:function(){s.debug&&(n.performance.log(arguments[0]),n.debug=Function.prototype.bind.call(console.log,console,s.moduleName+":"))},verbose:function(){s.verbose&&s.debug&&(n.performance.log(arguments[0]),n.verbose=Function.prototype.bind.call(console.info,console,s.moduleName+":"))},error:function(){n.error=Function.prototype.bind.call(console.log,console,s.moduleName+":")},performance:{log:function(e){var t,o,i;s.performance&&(t=(new Date).getTime(),i=l||t,o=t-i,l=t,u.push({Element:v,Name:e,"Execution Time":o}),clearTimeout(n.performance.timer),n.performance.timer=setTimeout(n.performance.display,100))},display:function(){var t=s.moduleName,n=(s.moduleName+": "+c+"("+r.size()+" elements)",0);c&&(t+="Performance ("+c+")"),(console.group!==o||console.table!==o)&&u.length>0&&(console.groupCollapsed(t),console.table?(e.each(u,function(e,t){n+=t["Execution Time"]}),console.table(u)):e.each(u,function(e,t){n+=t["Execution Time"]}),console.log("Total Execution Time:",n+"ms"),console.groupEnd(),u=[],l=!1)}},invoke:function(t,i,a){var r,s;return i=i||f,a=v||a,"string"==typeof t&&b!==o&&(t=t.split("."),r=t.length-1,e.each(t,function(t,i){return e.isPlainObject(b[i])&&t!=r?(b=b[i],!0):b[i]!==o?(s=b[i],!0):(n.error(C.method),!1)})),e.isFunction(s)?(n.verbose("Executing invoked function",s),s.apply(a,i)):s||!1}},m?(b===o&&n.initialize(),a=n.invoke(d)):(b!==o&&n.destroy(),n.initialize())}),a?a:this},e.fn.form.settings={moduleName:"Validate Form Module",debug:!0,verbose:!1,namespace:"validate",animateSpeed:150,inlineError:!1,on:"submit",onValid:function(){},onInvalid:function(){},onSuccess:function(){return!0},onFailure:function(){return!1},metadata:{validate:"validate"},errors:{method:"The method you called is not defined.",noFields:"No validation object specified."},selector:{group:".field",prompt:".prompt",field:"input, textarea, select"},className:{error:"error",prompt:"prompt"},defaults:{firstName:{identifier:"first-name",rules:[{type:"empty",prompt:"Please enter your first name"}]},lastName:{identifier:"last-name",rules:[{type:"empty",prompt:"Please enter your last name"}]},username:{identifier:"username",rules:[{type:"email",prompt:"Please enter a username"}]},email:{identifier:"email",rules:[{type:"empty",prompt:"Please enter your email"},{type:"email",prompt:"Please enter a valid email"}]},password:{identifier:"password",rules:[{type:"empty",prompt:"Please enter a password"},{type:"length[6]",prompt:"Your password must be at least 6 characters"}]},passwordConfirm:{identifier:"password-confirm",rules:[{type:"empty",prompt:"Please confirm your password"},{identifier:"password-confirm",type:"match[password]",prompt:"Please verify password matches"}]},terms:{identifier:"terms",rules:[{type:"checked",prompt:"You must agree to the terms and conditions"}]}},rules:{empty:function(e){return!(e===o||""===e)},email:function(e){var t=RegExp("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?");return t.test(e)},length:function(e,t){return e!==o?e.length>=t:!1},not:function(e,t){return e!=t},maxLength:function(e,t){return e!==o?t>=e.length:!1},match:function(t,n){var i,a=e(this);return a.find("#"+n).size()>0?i=a.find("#"+n).val():a.find("[name="+n+"]").size()>0?i=a.find("[name="+n+"]").val():a.find('[data-validate="'+n+'"]').size()>0&&(i=a.find('[data-validate="'+n+'"]').val()),i!==o?""+t==""+i:!1},url:function(e){var t=/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;return t.test(e)}}}})(jQuery,window,document);