UNPKG

yavjs

Version:

Lightweight JavaScript form validation fork from validatejs

1 lines 11.1 kB
!function(e,t,a){var n={messages:{en:{required:"The %s field is required.",matches:"The %s field does not match the %s field.",default:"The %s field is still set to default, please change.",valid_email:"The %s field must contain a valid email address.",valid_emails:"The %s field must contain all valid email addresses.",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.",decimal:"The %s field must contain a decimal number.",is_natural:"The %s field must contain only positive numbers.",is_natural_no_zero:"The %s field must contain a number greater than zero.",valid_ip:"The %s field must contain a valid IP.",valid_base64:"The %s field must contain a base64 string.",valid_credit_card:"The %s field must contain a valid credit card number.",is_file_type:"The %s field must contain only %s files.",valid_url:"The %s field must contain a valid URL.",greater_than_date:"The %s field must contain a more recent date than %s.",less_than_date:"The %s field must contain an older date than %s.",greater_than_or_equal_date:"The %s field must contain a date that's at least as recent as %s.",less_than_or_equal_date:"The %s field must contain a date that's %s or older."},fr:{required:"Le champ %s est requis.",matches:"Le champ %s ne correspond pas au champ %s.",default:"Le champ %s est a la valeur par défaut, s.v.p corriger.",valid_email:"Le champ %s doit contenir un courriel valide.",valid_emails:"Les champs %s doivent contenir des adresses valides.",min_length:"Le champ %s doit avoir un minimum de %s caractères.",max_length:"Le champ %s ne doit pas dépasser %s caractères.",exact_length:"Le champ %s doit avoir exactement %s caractères.",greater_than:"Le champ %s doit contenir un nombre plus grand que %s.",less_than:"Le champ %s doit contenir un nombre plus petit que %s.",alpha:"Le champ %s doit contenir seulement des lettres.",alpha_numeric:"Le champ %s doit contenir seulement des lettres et des chiffres.",alpha_dash:"Le champ %s doit contenir seulement des lettres, chiffres, barres de soulignement, et des tirets.",numeric:"Le champ %s doit contenir seulement des chiffres.",integer:"Le champ %s doit contenir seulement des entier.",decimal:"Le champ %s doit contenir seulement des décimales.",is_natural:"Le champ %s doit contenir seulement des nombres positifs.",is_natural_no_zero:"Le champ %s doit contenir un nombre plus grand que zéro.",valid_ip:"Le champ %s doit contenir un adresse ip valide.",valid_base64:"Le champ %s doit contenir un chaine base64.",valid_credit_card:"Le champ %s un numéro de carte de crédit valide.",is_file_type:"Le champ %s doit contenir seulement %s fichier.",valid_url:"Le champ %s doit contenir un url valide.",greater_than_date:"Le champ %s doit contenir une date plus récente que %s.",less_than_date:"Le champ %s une date antérieure à %s.",greater_than_or_equal_date:"Le champ %s doit contenir une date plus récente ou égale à %s.",less_than_or_equal_date:"Le champ %s doit contenir une date antérieure ou égale à %s."}},callback:function(e){},locale:"en"},s=/^(.+?)\[(.+)\]$/,i=/^[0-9]+$/,r=/^\-?[0-9]+$/,l=/^\-?[0-9]*\.?[0-9]+$/,o=/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/,d=/^[a-z]+$/i,u=/^[a-z0-9]+$/i,c=/^[a-z0-9_\-]+$/i,h=/^[0-9]+$/i,m=/^[1-9][0-9]*$/i,p=/^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})$/i,f=/[^a-zA-Z0-9\/\+=]/i,v=/^[\d\-\s]+$/,_=/^((http|https):\/\/(\w+:{0,1}\w*@)?(\S+)|)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$/,g=function(e,t,a,s){this.locale=s||n.locale,this.callback=a||n.callback,this.errors=[],this.fields={},this.form=this._formByNameOrNode(e)||{},this.messages={},this.handlers={},this.conditionals={},this.setRules(t),this._autoloadField();var i=this.form.onsubmit;this.form.onsubmit=function(e){return function(t){try{return e.validateForm(t)&&(void 0===i||i())}catch(e){console.error(e.message)}}}(this)},y=function(e,t){var a;{if(!(e.length>0)||"radio"!==e[0].type&&"checkbox"!==e[0].type)return e[t];for(a=0,elementLength=e.length;a<elementLength;a++)if(e[a].checked)return e[a][t]}};g.prototype.setMessage=function(e,t,a){return this.messages[e][t]=a,this},g.prototype.addLocaleMessage=function(e,t){return this.messages[e]=t,this},g.prototype.setRules=function(e){for(var t=0,a=e.length;t<a;t++){var n=e[t];if((n.name||n.names)&&n.rules)if(n.names)for(var s=0,i=n.names.length;s<i;s++)this._addField(n,n.names[s]);else this._addField(n,n.name);else console.warn("validate.js: The following field is being skipped due to a misconfiguration:"),console.warn(n),console.warn("Check to ensure you have properly configured a name and rules for this field")}return this},g.prototype.registerCallback=function(e,t){return e&&"string"==typeof e&&t&&"function"==typeof t&&(this.handlers[e]=t),this},g.prototype.registerConditional=function(e,t){return e&&"string"==typeof e&&t&&"function"==typeof t&&(this.conditionals[e]=t),this},g.prototype._formByNameOrNode=function(e){return"object"==typeof e?e:t.forms[e]},g.prototype._autoloadField=function(){for(var e=t.querySelectorAll("[data-yavjs-rules]"),a=[],n=0;n<e.length;n++){var s=e[n],i={name:s.name,display:s.getAttribute("data-yavjs-display")||s.name||s.id,rules:s.getAttribute("data-yavjs-rules"),depends:s.getAttribute("data-yavjs-depends"),on:s.getAttribute("data-yavjs-on")};a.push(i)}a.length>0&&this.setRules(a)},g.prototype._addField=function(a,n){if(this.fields[n]={name:n,display:a.display||n,rules:a.rules,depends:a.depends,id:null,element:null,type:null,value:null,checked:null},void 0!==a.on&&null!=a.on){console.log(a.on);var s=t.getElementsByName(n),i=a.on;"string"==typeof i&&(i=JSON.parse(i));for(var r=this,l=0;l<s.length;l++)s[l].addEventListener(i.event,function(t){e[i.callback](t,r.validateField(a))},!1)}},g.prototype.validateForm=function(e){this.errors=[];for(var t in this.fields)if(this.fields.hasOwnProperty(t)){var a=this.fields[t]||{};a.depends&&"function"==typeof a.depends?a.depends.call(this,a)&&this.validateField(a):a.depends&&"string"==typeof a.depends&&this.conditionals[a.depends]?this.conditionals[a.depends].call(this,a)&&this.validateField(a):this.validateField(a)}return"function"==typeof this.callback&&this.callback(this.errors,e),this.errors.length>0&&(e&&e.preventDefault?e.preventDefault():event&&(event.returnValue=!1)),!0},g.prototype.getFieldFromName=function(e){var a=t.getElementsByName(e.name);return("checkbox"!==e.type||"radio"!=e.type)&&a.length>0?a[0]:a},g.prototype.validateField=function(e){if("string"==typeof e&&(e=this.fields[e]),element=this.form[e.name]||this.getFieldFromName(e)||t.getElementById(e.name),element&&void 0!==element){e.id=y(element,"id"),e.element=element,e.type=element.length>0?element[0].type:element.type,e.value=y(element,"value"),e.checked=y(element,"checked");var a,n,i,r=e.rules.split("|"),l=e.rules.indexOf("required"),o=!e.value||""===e.value||void 0===e.value;for(a=0,i=r.length;a<i;a++){var d=r[a],u=null,c=!1,h=s.exec(d);if((-1!==l||-1!==d.indexOf("!callback_")||!o)&&(h&&(d=h[1],u=h[2]),"!"===d.charAt(0)&&(d=d.substring(1,d.length)),"function"==typeof this._hooks[d]?this._hooks[d].apply(this,[e,u])||(c=!0):"callback_"===d.substring(0,9)&&(d=d.substring(9,d.length),"function"==typeof this.handlers[d]&&!1===this.handlers[d].apply(this,[e.value,u,e])&&(c=!0)),c)){var m=this.getErrorMessage(e,d),p="An error has occurred with the "+e.display+" field.";m&&(p=m.replace("%s",e.display),u&&(p=p.replace("%s",this.fields[u]?this.fields[u].display:u)));var f;for(n=0;n<this.errors.length;n+=1)e.id===this.errors[n].id&&(f=this.errors[n]);var v=f||{id:e.id,display:e.display,element:e.element,name:e.name,message:p,messages:[],rule:d};return v.messages.push(p),f||this.errors.push(v),v}}}return!0},g.prototype.getErrorMessage=function(e,t){return this.messages[this.locale]?this.messages[this.locale][t]||n.messages[this.locale][t]:n.messages[this.locale][t]},g.prototype._getValidDate=function(e){if(!e.match("today")&&!e.match(/\d{4}-\d{1,2}-\d{1,2}/))return!1;var t,a=new Date;return e.match("today")||(t=e.split("-"),a.setFullYear(t[0]),a.setMonth(t[1]-1),a.setDate(t[2])),a},g.prototype._hooks={required:function(e){var t=e.value;return"checkbox"===e.type||"radio"===e.type?!0===e.checked:null!==t&&""!==t},default:function(e,t){return e.value!==t},matches:function(e,a){var n=this.form[a]||t.getElementsByName(a)[0];return!!n&&e.value===n.value},valid_email:function(e){return o.test(e.value)},valid_emails:function(e){for(var t=e.value.split(/\s*,\s*/g),a=0,n=t.length;a<n;a++)if(!o.test(t[a]))return!1;return!0},min_length:function(e,t){return!!i.test(t)&&e.value.length>=parseInt(t,10)},max_length:function(e,t){return!!i.test(t)&&e.value.length<=parseInt(t,10)},exact_length:function(e,t){return!!i.test(t)&&e.value.length===parseInt(t,10)},greater_than:function(e,t){return!!l.test(e.value)&&parseFloat(e.value)>parseFloat(t)},less_than:function(e,t){return!!l.test(e.value)&&parseFloat(e.value)<parseFloat(t)},alpha:function(e){return d.test(e.value)},alpha_numeric:function(e){return u.test(e.value)},alpha_dash:function(e){return c.test(e.value)},numeric:function(e){return i.test(e.value)},integer:function(e){return r.test(e.value)},decimal:function(e){return l.test(e.value)},is_natural:function(e){return h.test(e.value)},is_natural_no_zero:function(e){return m.test(e.value)},valid_ip:function(e){return p.test(e.value)},valid_base64:function(e){return f.test(e.value)},valid_url:function(e){return _.test(e.value)},valid_credit_card:function(e){if(!v.test(e.value))return!1;for(var t=0,a=0,n=!1,s=e.value.replace(/\D/g,""),i=s.length-1;i>=0;i--){var r=s.charAt(i);a=parseInt(r,10),n&&(a*=2)>9&&(a-=9),t+=a,n=!n}return t%10==0},is_file_type:function(e,t){if("file"!==e.type)return!0;var a=e.value.substr(e.value.lastIndexOf(".")+1),n=t.split(","),s=!1,i=0,r=n.length;for(i;i<r;i++)a.toUpperCase()==n[i].toUpperCase()&&(s=!0);return s},greater_than_date:function(e,t){var a=this._getValidDate(e.value),n=this._getValidDate(t);return!(!n||!a)&&a>n},less_than_date:function(e,t){var a=this._getValidDate(e.value),n=this._getValidDate(t);return!(!n||!a)&&a<n},greater_than_or_equal_date:function(e,t){var a=this._getValidDate(e.value),n=this._getValidDate(t);return!(!n||!a)&&a>=n},less_than_or_equal_date:function(e,t){var a=this._getValidDate(e.value),n=this._getValidDate(t);return!(!n||!a)&&a<=n}},e.FormValidator=g,e.Yavjs=g}(window,document),"undefined"!=typeof module&&module.exports&&(module.exports=FormValidator);