/*! * # Semantic UI 2.0.0 - Form Validation * http://github.com/semantic-org/semantic-ui/ * * * Copyright 2014 Contributors * Released under the MIT license * http://opensource.org/licenses/MIT * */ !function(e,t,n,i){"use strict";e.fn.form=function(t){var r,o=e(this),a=o.selector||"",s=(new Date).getTime(),l=[],c=arguments[0],u=arguments[1],d="string"==typeof c,f=[].slice.call(arguments,1);return o.each(function(){var p,g,m,v,h,b,y,k,x,w,C,E,S,R,w,A,F,O,T,z=e(this),D=this,j=[],V=!1;T={initialize:function(){T.get.settings(),d?T.invoke(c):(T.verbose("Initializing form validation",z,k),T.bindEvents(),T.set.defaults(),T.instantiate())},instantiate:function(){T.verbose("Storing instance of module",T),O=T,z.data(A,T)},destroy:function(){T.verbose("Destroying previous module",O),T.removeEvents(),z.removeData(A)},refresh:function(){T.verbose("Refreshing selector cache"),p=z.find(E.field),g=z.find(E.group),m=z.find(E.message),v=z.find(E.prompt),h=z.find(E.submit),b=z.find(E.clear),y=z.find(E.reset)},submit:function(){T.verbose("Submitting form",z),z.submit()},attachEvents:function(t,n){n=n||"submit",e(t).on("click"+F,function(e){T[n](),e.preventDefault()})},bindEvents:function(){T.verbose("Attaching form events"),z.on("submit"+F,T.validate.form).on("blur"+F,E.field,T.event.field.blur).on("click"+F,E.submit,T.submit).on("click"+F,E.reset,T.reset).on("click"+F,E.clear,T.clear),k.keyboardShortcuts&&z.on("keydown"+F,E.field,T.event.field.keydown),p.each(function(){var t=e(this),n=t.prop("type"),i=T.get.changeEvent(n,t);e(this).on(i+F,T.event.field.change)})},clear:function(){p.each(function(){var t=e(this),n=t.parent(),i=t.closest(g),r=i.find(E.prompt),o=t.data(C.defaultValue)||"",a=n.is(E.uiCheckbox),s=n.is(E.uiDropdown),l=i.hasClass(S.error);l&&(T.verbose("Resetting error on field",i),i.removeClass(S.error),r.remove()),s?(T.verbose("Resetting dropdown value",n,o),n.dropdown("clear")):a?n.checkbox("uncheck"):(T.verbose("Resetting field value",t,o),t.val(""))})},reset:function(){p.each(function(){var t=e(this),n=t.parent(),i=t.closest(g),r=i.find(E.prompt),o=t.data(C.defaultValue)||"",a=n.is(E.uiCheckbox),s=n.is(E.uiDropdown),l=i.hasClass(S.error);l&&(T.verbose("Resetting error on field",i),i.removeClass(S.error),r.remove()),s?(T.verbose("Resetting dropdown value",n,o),n.dropdown("restore defaults")):a?(T.verbose("Resetting checkbox value",n,o),n.checkbox(o===!0?"check":"uncheck")):(T.verbose("Resetting field value",t,o),t.val(o))})},removeEvents:function(){z.off(F),p.off(F),h.off(F),p.off(F)},event:{field:{keydown:function(t){var n=e(this),i=t.which,r={enter:13,escape:27};i==r.escape&&(T.verbose("Escape key pressed blurring field"),n.blur()),!t.ctrlKey&&i==r.enter&&n.is(E.input)&&n.not(E.checkbox).length>0&&(h.addClass(S.pressed),V||(n.one("keyup"+F,T.event.field.keyup),T.submit(),T.debug("Enter pressed on input submitting form")),V=!0)},keyup:function(){V=!1,h.removeClass(S.pressed)},blur:function(){var t=e(this),n=t.closest(g);n.hasClass(S.error)?(T.debug("Revalidating field",t,T.get.validation(t)),T.validate.field(T.get.validation(t))):("blur"==k.on||"change"==k.on)&&T.validate.field(T.get.validation(t))},change:function(){var t=e(this),n=t.closest(g);("change"==k.on||n.hasClass(S.error)&&k.revalidate)&&(clearTimeout(T.timer),T.timer=setTimeout(function(){T.debug("Revalidating field",t,T.get.validation(t)),T.validate.field(T.get.validation(t))},k.delay))}}},get:{changeEvent:function(e,t){return"checkbox"==e||"radio"==e||"hidden"==e||t.is("select")?"change":T.get.inputEvent()},inputEvent:function(){return n.createElement("input").oninput!==i?"input":n.createElement("input").onpropertychange!==i?"propertychange":"keyup"},settings:function(){if(e.isPlainObject(t)){var n=Object.keys(t),r=n.length>0?t[n[0]].identifier!==i:!1;r?(k=e.extend(!0,{},e.fn.form.settings,u),x=e.extend({},e.fn.form.settings.defaults,t),T.error(k.error.oldSyntax,D),T.verbose("Extending settings from legacy parameters",x,k)):(k=e.extend(!0,{},e.fn.form.settings,t),x=e.extend({},e.fn.form.settings.defaults,k.fields),T.verbose("Extending settings",x,k))}else k=e.fn.form.settings,x=e.fn.form.settings.defaults,T.verbose("Using default form validation",x,k);w=k.namespace,C=k.metadata,E=k.selector,S=k.className,R=k.error,A="module-"+w,F="."+w,O=z.data(A),T.refresh()},field:function(t){return T.verbose("Finding field with identifier",t),p.filter("#"+t).length>0?p.filter("#"+t):p.filter('[name="'+t+'"]').length>0?p.filter('[name="'+t+'"]'):p.filter('[name="'+t+'[]"]').length>0?p.filter('[name="'+t+'[]"]'):p.filter("[data-"+C.validate+'="'+t+'"]').length>0?p.filter("[data-"+C.validate+'="'+t+'"]'):e("")},fields:function(t){var n=e();return e.each(t,function(e,t){n=n.add(T.get.field(t))}),n},validation:function(t){var n;return e.each(x,function(e,i){T.get.field(i.identifier).get(0)==t.get(0)&&(n=i)}),n||!1},value:function(e){var t,n=[];return n.push(e),t=T.get.values.call(D,n),t[e]},values:function(t){var n=e.isArray(t)?T.get.fields(t):p,i={};return n.each(function(t,n){var r=e(n),o=(r.prop("type"),r.prop("name")),a=r.val(),s=r.is(E.checkbox),l=r.is(E.radio),c=-1!==o.indexOf("[]"),u=s?r.is(":checked"):!1;if(o)if(c)if(o=o.replace("[]",""),i[o]||(i[o]=[]),s){if(!u)return T.debug("Omitted unchecked checkbox",r),!0;i[o].push(a)}else i[o].push(a);else if(l)u&&(i[o]=a);else if(s){if(!u)return T.debug("Omitted unchecked checkbox",r),!0;i[o]=!0}else i[o]=a}),i}},has:{field:function(e){return T.verbose("Checking for existence of a field with identifier",e),p.filter("#"+e).length>0?!0:p.filter('[name="'+e+'"]').length>0?!0:p.filter("[data-"+C.validate+'="'+e+'"]').length>0?!0:!1}},add:{prompt:function(t,n){var r=T.get.field(t),o=r.closest(g),a=o.children(E.prompt),s=0!==a.length;n="string"==typeof n?[n]:n,T.verbose("Adding field error state",t),o.addClass(S.error),k.inline&&(s||(a=k.templates.prompt(n),a.appendTo(o)),a.html(n[0]),s?T.verbose("Inline errors are disabled, no inline error added",t):k.transition&&e.fn.transition!==i&&z.transition("is supported")?(T.verbose("Displaying error with css transition",k.transition),a.transition(k.transition+" in",k.duration)):(T.verbose("Displaying error with fallback javascript animation"),a.fadeIn(k.duration)))},errors:function(e){T.debug("Adding form error messages",e),m.html(k.templates.error(e))}},remove:{prompt:function(t){var n=T.get.field(t.identifier),r=n.closest(g),o=r.children(E.prompt);r.removeClass(S.error),k.inline&&o.is(":visible")&&(T.verbose("Removing prompt for field",t),k.transition&&e.fn.transition!==i&&z.transition("is supported")?o.transition(k.transition+" out",k.duration,function(){o.remove()}):o.fadeOut(k.duration,function(){o.remove()}))}},set:{success:function(){z.removeClass(S.error).addClass(S.success)},defaults:function(){p.each(function(){var t=e(this),n=t.filter(E.checkbox).length>0,i=n?t.is(":checked"):t.val();t.data(C.defaultValue,i)})},error:function(){z.removeClass(S.success).addClass(S.error)},value:function(e,t){var n={};return n[e]=t,T.set.values.call(D,n)},values:function(t){e.isEmptyObject(t)||(e.each(t,function(t,n){var i,r=T.get.field(t),o=r.parent(),a=e.isArray(n),s=o.is(E.uiCheckbox),l=o.is(E.uiDropdown),c=r.is(E.radio)&&s,u=r.length>0;u&&(a&&s?(T.verbose("Selecting multiple",n,r),o.checkbox("uncheck"),e.each(n,function(e,t){i=r.filter('[value="'+t+'"]'),o=i.parent(),i.length>0&&o.checkbox("check")})):c?(T.verbose("Selecting radio value",n,r),r.filter('[value="'+n+'"]').parent(E.uiCheckbox).checkbox("check")):s?(T.verbose("Setting checkbox value",n,o),o.checkbox(n===!0?"check":"uncheck")):l?(T.verbose("Setting dropdown value",n,o),o.dropdown("set selected",n)):(T.verbose("Setting field value",n,r),r.val(n)))}),T.validate.form())}},validate:{form:function(t){var n=!0;return V?!1:(j=[],e.each(x,function(e,t){T.validate.field(t)||(n=!1)}),n?(T.debug("Form has no validation errors, submitting"),T.set.success(),k.onSuccess.call(D,t)):(T.debug("Form has errors"),T.set.error(),k.inline||T.add.errors(j),z.data("moduleApi")!==i&&t.stopImmediatePropagation(),k.onFailure.call(D,j)))},field:function(t){var n=T.get.field(t.identifier),r=!0,o=[];return n.prop("disabled")?(T.debug("Field is disabled. Skipping",t.identifier),r=!0):t.optional&&""===e.trim(n.val())?(T.debug("Field is optional and empty. Skipping",t.identifier),r=!0):t.rules!==i&&e.each(t.rules,function(e,n){T.has.field(t.identifier)&&!T.validate.rule(t,n)&&(T.debug("Field is invalid",t.identifier,n.type),o.push(n.prompt),r=!1)}),r?(T.remove.prompt(t,o),k.onValid.call(n),!0):(j=j.concat(o),T.add.prompt(t.identifier,o),k.onInvalid.call(n,o),!1)},rule:function(t,n){var r,o,a=T.get.field(t.identifier),s=n.type,l=a.val(),c=/\[(.*)\]/i,u=c.exec(s),d=!0;return l=e.trim(a.val()+""),u!==i&&null!==u?(r=""+u[1],o=s.replace(u[0],""),d=k.rules[o].call(D,l,r)):d=k.rules[s].call(a,l),d}},setting:function(t,n){if(e.isPlainObject(t))e.extend(!0,k,t);else{if(n===i)return k[t];k[t]=n}},internal:function(t,n){if(e.isPlainObject(t))e.extend(!0,T,t);else{if(n===i)return T[t];T[t]=n}},debug:function(){k.debug&&(k.performance?T.performance.log(arguments):(T.debug=Function.prototype.bind.call(console.info,console,k.name+":"),T.debug.apply(console,arguments)))},verbose:function(){k.verbose&&k.debug&&(k.performance?T.performance.log(arguments):(T.verbose=Function.prototype.bind.call(console.info,console,k.name+":"),T.verbose.apply(console,arguments)))},error:function(){T.error=Function.prototype.bind.call(console.error,console,k.name+":"),T.error.apply(console,arguments)},performance:{log:function(e){var t,n,i;k.performance&&(t=(new Date).getTime(),i=s||t,n=t-i,s=t,l.push({Name:e[0],Arguments:[].slice.call(e,1)||"",Element:D,"Execution Time":n})),clearTimeout(T.performance.timer),T.performance.timer=setTimeout(T.performance.display,500)},display:function(){var t=k.name+":",n=0;s=!1,clearTimeout(T.performance.timer),e.each(l,function(e,t){n+=t["Execution Time"]}),t+=" "+n+"ms",a&&(t+=" '"+a+"'"),o.length>1&&(t+=" ("+o.length+")"),(console.group!==i||console.table!==i)&&l.length>0&&(console.groupCollapsed(t),console.table?console.table(l):e.each(l,function(e,t){console.log(t.Name+": "+t["Execution Time"]+"ms")}),console.groupEnd()),l=[]}},invoke:function(t,n,o){var a,s,l,c=O;return n=n||f,o=D||o,"string"==typeof t&&c!==i&&(t=t.split(/[\. ]/),a=t.length-1,e.each(t,function(n,r){var o=n!=a?r+t[n+1].charAt(0).toUpperCase()+t[n+1].slice(1):t;if(e.isPlainObject(c[o])&&n!=a)c=c[o];else{if(c[o]!==i)return s=c[o],!1;if(!e.isPlainObject(c[r])||n==a)return c[r]!==i?(s=c[r],!1):!1;c=c[r]}})),e.isFunction(s)?l=s.apply(o,n):s!==i&&(l=s),e.isArray(r)?r.push(l):r!==i?r=[r,l]:l!==i&&(r=l),s}},T.initialize()}),r!==i?r:this},e.fn.form.settings={name:"Form",namespace:"form",debug:!1,verbose:!1,performance:!0,fields:!1,keyboardShortcuts:!0,on:"submit",inline:!1,delay:200,revalidate:!0,transition:"scale",duration:200,onValid:function(){},onInvalid:function(){},onSuccess:function(){return!0},onFailure:function(){return!1},metadata:{defaultValue:"default",validate:"validate"},selector:{checkbox:'input[type="checkbox"], input[type="radio"]',clear:".clear",field:"input, textarea, select",group:".field",input:"input",message:".error.message",prompt:".prompt.label",radio:'input[type="radio"]',reset:'.reset:not([type="reset"])',submit:'.submit:not([type="submit"])',uiCheckbox:".ui.checkbox",uiDropdown:".ui.dropdown"},className:{error:"error",label:"ui prompt label",pressed:"down",success:"success"},error:{oldSyntax:"Starting in 2.0 forms now only take a single settings object. Validation settings converted to new syntax automatically.",method:"The method you called is not defined."},templates:{error:function(t){var n='",e(n)},prompt:function(t){return e("
").addClass("ui red pointing prompt label").html(t[0])}},rules:{checked:function(){return e(this).filter(":checked").length>0},contains:function(e,t){return t=t.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g,"\\$&"),-1!==e.search(new RegExp(t,"i"))},containsExactly:function(e,t){return t=t.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g,"\\$&"),-1!==e.search(new RegExp(t))},email:function(e){var t=new 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])?","i");return t.test(e)},empty:function(t){return!(t===i||""===t||e.isArray(t)&&0==t.length)},integer:function(e,t){var n,r,o,a=/^\-?\d+$/;return t===i||""===t||".."===t||(-1==t.indexOf("..")?a.test(t)&&(n=r=t-0):(o=t.split("..",2),a.test(o[0])&&(n=o[0]-0),a.test(o[1])&&(r=o[1]-0))),a.test(e)&&(n===i||e>=n)&&(r===i||r>=e)},is:function(e,t){return t="string"==typeof t?t.toLowerCase():t,e="string"==typeof e?e.toLowerCase():e,e==t},isExactly:function(e,t){return e==t},length:function(e,t){return e!==i?e.length>=t:!1},match:function(t,n){var r,o=e(this);return o.find("#"+n).length>0?r=o.find("#"+n).val():o.find('[name="'+n+'"]').length>0?r=o.find('[name="'+n+'"]').val():o.find('[data-validate="'+n+'"]').length>0&&(r=o.find('[data-validate="'+n+'"]').val()),r!==i?t.toString()==r.toString():!1},maxCount:function(t,n){return t=t.split(","),e.isArray(t)&&t.length<=n},exactCount:function(t,n){return t=t.split(","),e.isArray(t)&&t.length==n},minCount:function(t,n){return t=t.split(","),e.isArray(t)&&t.length>=n},maxLength:function(e,t){return e!==i?e.length<=t:!1},not:function(e,t){return e="string"==typeof e?e.toLowerCase():e,t="string"==typeof t?t.toLowerCase():t,e!=t},notExactly:function(e,t){return e!=t},url:function(e){var t=/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;return t.test(e)}}}}(jQuery,window,document);