-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathform-validator.min.js
More file actions
16 lines (16 loc) · 5.83 KB
/
form-validator.min.js
File metadata and controls
16 lines (16 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* JavaScript Form Validator
*
* Copyright (c) 2014 Alejandro Perez Martin (AlePerez92)
*
* @name JavaScript Form Validator (Form-Validator.js)
* @description JavaScript form Validator, offers validation while user is typing and no other libraries dependency!
* @license GNU General Public License (GPL), https://www.gnu.org/licenses/gpl.html
* @version 1.0.0
* @build March 10, 2015
* @repository http://github.com/alejandroperezmartin/javascript-form-validator
*
* @author Alejandro Perez Martin
* @authorUrl https://www.linkedin.com/in/aleperez92
*/
!function(e,t){"use strict";var r={alphabetic:"This field only allows alphabetical characters and spaces",alphanumeric:"The %s field only allow numbers, letters, spaces and dashes",address:"Please enter a valid address",defaultError:"This field is invalid",email:"You entered an invalid email",equal_to:"The %s field must to be equal to %a",exact_length:"The %s field must be exactly %a characters in length",greater_than:"The %s field must be greater than %a",integer:"The %s field only accepts integers",less_than:"The %s field must be less than %a",match:"The %s field should match '%a'",max_length:"The %s field can't exceed %a charcters",min_length:"The %s field must be at least %a characters in length",name:"The %s field only allows alphabetic characters, spaces and dashes",required:"This field is required",spanish_dni:"The %s field only allows 8 numbers and 1 character",spanish_mobile:"The %s field only allows 9 numbers starting by 6 or 7",spanish_phone:"The %s field only allows 9 numbers starting by 6, 7, 8 or 9",spanish_postal:"Please enter a valid postal code",url:"This is not a valid URL",username:"The %s field only allows numbers, letters, '-' and '_'"},a=/^[A-z0-9áéíóúñçÁÉÍÓÚÑÇ]+$/,n=/^[A-záéíóúñçÁÉÍÓÚÑÇ]+$/,s=/^[A-z0-9 áéíóúñçÁÉÍÓÚÑÇ'´.,-ºª]+$/,l=/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/,i=/^[0-9]+$/,o=/^([A-záéíóúñçÁÉÍÓÚÑÇ]+[ -´']?)+$/,u=/^[0-9]{8}[a-zA-Z]{1}$/,f=/^[67]{1}[0-9]{8}$/,d=/^[6789]{1}[0-9]{8}$/,c=/^[0-5]{1}[0-9]{4}$/,h=/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/,m=/^[a-zA-Z0-9_-]+$/,v=function(e,t){return RegExp(" "+t+" ").test(" "+e.className+" ")},p=function(e,t){v(e,t)||(e.className+=" "+t)},g=function(e,t){var r=" "+e.className.replace(/[\t\r\n]/g," ")+" ";if(v(e,t)){for(;r.indexOf(" "+t+" ")>=0;)r=r.replace(" "+t+" "," ");e.className=r.replace(/^\s+|\s+$/g,"")}},y=function(e,t){var r=e.nextSibling;r&&v(r,t)&&r.remove()},_=function(e,t){e.insertAdjacentHTML("afterend",t)},b=function(e,t,r){this.fields=t,this.form=this.getFormByName(e);var a=this;if("live_validation"===r)for(var n=0,s=this.fields.length;s>n;n+=1){var l=a.fields[n];a.form[l.name].length&&"radio"===a.form[l.name][0].type||!function(e){a.form[e.name].addEventListener("blur",function(){a.validateField(e),this.addEventListener("keyup",function(){a.validateField(e)})})}(l)}this.form.onsubmit=function(e){if(!a.validateForm(a.fields)&&(e.preventDefault(),showErrorsInfo&&!document.getElementsByClassName("js-form-error-message")[0])){var t=document.createElement("DIV");t.className="js-form-error-message",t.innerHTML='<span class="js-form-error-message-title">Ooooops!</span>This form seems to have errors.',a.form.insertBefore(t,a.form.firstChild)}}};b.prototype.getFormByName=function(e){return document.forms[e]||{}},b.prototype.validateForm=function(e){if(e.length<=0)return!1;for(var t=!0,r=0,a=e.length;a>r;r++)this.validateField(e[r])||(t=!1);return t},b.prototype.validateField=function(e){if(!e)return!1;for(var a=this.form[e.name],n=-1!==e.rules.indexOf("required"),s=!a.value||""===a.value||a.value===t,l=0,i=e.rules.length;i>l;l++){var o=e.rules[l].split("=")[0],u=e.rules[l].split("=")[1];if(!n&&s)break;if(u){if(!this.validators[o](a,u))return a.length&&"radio"===a[0].type&&(a=a[a.length-1]),g(a,"js-form-field-valid"),p(a,"js-form-field-invalid"),y(a,"js-form-field-error"),_(a,'<span class="js-form-field-error">'+(r[o]?r[o].replace("%s",e.name).replace("%a",u):r.defaultError)+"</span>"),!1}else if(!this.validators[o](a))return a.length&&"radio"===a[0].type&&(a=a[a.length-1]),g(a,"js-form-field-valid"),p(a,"js-form-field-invalid"),y(a,"js-form-field-error"),_(a,'<span class="js-form-field-error">'+(r[o]?r[o].replace("%s",e.name):r.defaultError)+"</span>"),!1}return a.length&&"radio"===a[0].type&&(a=a[a.length-1]),y(a,"js-form-field-error"),g(a,"js-form-field-invalid"),!n&&s?g(a,"js-form-field-valid"):p(a,"js-form-field-valid"),!0},b.prototype.validators={address:function(e){return s.test(e.value.trim())},alphanumeric:function(e){return a.test(e.value.trim())},alphabetic:function(e){return n.test(e.value.trim())},email:function(e){return l.test(e.value)},equal_to:function(e,t){return i.test(t)&&e.value===parseInt(t)},exact_length:function(e,t){return i.test(t)&&e.value.trim().length===parseInt(t)},greater_than:function(e,t){return i.test(e.value)&&i.test(t)&&e.value>=parseInt(t)},integer:function(e){return i.test(e.value)},less_than:function(e,t){return i.test(e.value)&&i.test(t)&&e.value<=parseInt(t)},match:function(e,t){return a.test(e.value)&&a.test(t)&&e.value===t},max_length:function(e,t){return i.test(t)&&e.value.trim().length<=parseInt(t)},min_length:function(e,t){return i.test(t)&&e.value.trim().length>=parseInt(t)},name:function(e){return o.test(e.value)},required:function(e){return"checkbox"===e.type||"radio"===e.type?e.checked:null!==e.value.trim()&&""!==e.value.trim()},spanish_dni:function(e){if(u.test(e.value)){var t="TRWAGMYFPDXBNJZSQVHLCKET";return e.value.slice(-1).toUpperCase()===t[e.value.slice(0,8)%23]}return!1},spanish_phone:function(e){return d.test(e.value)},spanish_mobile:function(e){return f.test(e.value)},spanish_postal:function(e){return c.test(e.value)},url:function(e){return h.test(e.value)},username:function(e){return m.test(e.value)}},e.FormValidator=b}(window);