jquerysimpleformvalidation
Version:
A Simple jQuery Plugin for Form Validation.SimpleValidation is a simplified jquery plugin to achieve client side form validation.
7 lines • 5.18 kB
JavaScript
/**
* JQuery Simple Form Validation
* Copyright (c) 2017 Position2 Inc.
* Licensed under MIT (http://opensource.org/licenses/MIT)
* https://github.com/Position2/jQuery-Simple-Form-Validation
*/
!function(z){z.fn.simpleValidation=function(e,l){var C=z.extend({errorFieldClass:"error",errorMsgTag:"span",errorMsgClass:"errormsg",errorMsg:"Required Field",otherErrorMsg:{email:"Please enter valid email",companyemail:"Please enter company email",alphabet:"Please enter letters only",alphabetwithspace:"Please enter letters & space only",number:"Please enter numbers only",numberwithspace:"Please enter numbers & space only",alphanumeric:"Please don't enter any special character or space",alphanumericwithspace:"Please don't enter any special character",compare:"Please enter the same value again",minlength:"Please enter minimum {n} letters"},beforeSubmit:""},e);return z("body").on("click",C.errorMsgTag+"."+C.errorMsgClass,function(){z(this).fadeOut(function(){z(this).remove()})}),this.each(function(){var f=z(this),t=f.attr("data-sfv-ajax")||!1,h=f.attr("data-sfv-minlength")||0,r=z("input[data-sfv-required='yes'][type='checkbox'],input[data-sfv-required='yes'][type='radio']",f),s=z("input[data-sfv-required='yes'],input[data-sfv-validation]:not(input[data-sfv-required='yes']),input[data-sfv-regex]:not(input[data-sfv-required='yes']),input[data-sfv-compare]:not(input[data-sfv-required='yes']),input[data-sfv-minlength]:not(input[data-sfv-required='yes']),select[data-sfv-required='yes'],textarea[data-sfv-required='yes']",f).not(r),e=z("input[data-sfv-compare]",f),v=/^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/,p=/^([\w+\-\.]+@(?!gmail.com)(?!hotmail.com)(?!live.com)(?!outlook.com)(?!yahoo.com)(?!ymail.com)(?!rocketmail.com)(?!aol.com)(?!mac.comme.com)(?!icloud.com)(?!inbox.com)(?!sina.com)(?!qq.com)(?!foxmail.com)(?!163.com)(?!126.com)(?!189.cn 263.net)(?!yeah.net)(?!gmx.com)(?!gmx.net)(?!mail.com)(?!mail.ru)(?!rambler.ru)(?!lenta.ru)(?!autorambler.ru)(?!myrambler.ru)(?!ro.ru)(?!yandex.ru)(?!zoho.com)(?!msn.com)(?!webtown.com)(?!rediffmail.com)([\w\-]+\.)+[\w\-]{2,4})?$/,g=/^[A-Za-z]+$/,b=/^[0-9]+$/,y=/^[0-9a-zA-Z]+$/,M=/^[A-Za-z ]+$/,E=/^[0-9 ]+$/,x=/^[0-9a-zA-Z ]+$/,i=z("<"+C.errorMsgTag+"/>",{class:C.errorMsgClass});function w(e,a,t){var r=z(e);t||r.addClass(C.errorFieldClass),r.next("."+C.errorMsgClass).size()<=0&&i.clone().text(a).insertAfter(r)}function q(e){var a=z(e),t=a.next("."+C.errorMsgClass);a.removeClass(C.errorFieldClass),0<t.size()&&t.remove()}function o(e){var a=z(e),t=a.val().trim(),r=(a.attr("name"),a.attr("data-sfv-required")),s=a.attr("data-sfv-minlength")||0,i=a.attr("data-sfv-regex"),o=a.attr("data-sfv-compare"),n=z(o),l=z("[data-sfv-compare]",f),m=z(l.attr("data-sfv-compare")),c=""!=o&&void 0!==o?n.val().trim():"",d=a.attr("data-sfv-regEx-errorMsg"),u=a.attr("data-sfv-require-errorMsg");if(""!=t)if("companyemail"==a.attr("type")||"companyemail"==a.attr("data-sfv-validation"))v.test(t)?p.test(t)?q(a):w(a,d||C.otherErrorMsg.companyemail):w(a,d||C.otherErrorMsg.email);else if("email"==a.attr("type")||"email"==a.attr("data-sfv-validation"))v.test(t)?q(a):w(a,d||C.otherErrorMsg.email);else if("alpha"==a.attr("data-sfv-validation"))g.test(t)?q(a):w(a,d||C.otherErrorMsg.alphabet);else if("alphawithspace"==a.attr("data-sfv-validation"))M.test(t)?q(a):w(a,d||C.otherErrorMsg.alphabetwithspace);else if("number"==a.attr("data-sfv-validation"))b.test(t)?q(a):w(a,d||C.otherErrorMsg.number);else if("numberwithspace"==a.attr("data-sfv-validation"))E.test(t)?q(a):w(a,d||C.otherErrorMsg.numberwithspace);else if("alphanumeric"==a.attr("data-sfv-validation"))y.test(t)?q(a):w(a,d||C.otherErrorMsg.alphanumeric);else if("alphanumericwithspace"==a.attr("data-sfv-validation"))x.test(t)?q(a):w(a,d||C.otherErrorMsg.alphanumericwithspace);else if(""!=i&&void 0!==i)(i=new RegExp("^"+i+"$")).test(t)?q(a):w(a,d||C.errorMsg);else if(""!=o&&void 0!==o&&0<n.size()&&""!=c)t!=c?w(n,d||C.otherErrorMsg.compare):q(n);else if(m.attr("id")==a.attr("id")&&void 0!==m.attr("id"))t!=l.val()?w(a,d||C.otherErrorMsg.compare):q(a);else if(0<s||0<h){if(0<s&&t.length<s)w(a,(d||C.otherErrorMsg.minlength).replace("{n}",s));else if(s<=0&&0<h.length&&t.length<h){w(a,(d||C.otherErrorMsg.minlength).replace("{n}",h))}else q(a)}else q(a);else"yes"==r&&w(a,u||C.errorMsg)}function n(e){var a=z(e),t=a.attr("name"),r=a.attr("data-sfv-require-errorMsg");disSibElem=z("[name='"+t+"']",f),disSibLElem=disSibElem.last()[0].nextSibling,disSibElem.is(":checked")?q(disSibLElem):w(disSibLElem,r||C.errorMsg,!0)}0<e.size()&&(s=s.add(z(e.attr("data-sfv-compare")))),f.attr("novalidate",""),f.on("submit",function(e){var a=z(this);if(s.add(r).each(function(){var e=z(this).attr("type");"checkbox"==e||"radio"==e?n(this):o(this)}),z("."+C.errorFieldClass+":visible,."+C.errorMsgClass+":visible",z(this)).size()<=0)return"function"==typeof C.beforeSubmit?C.beforeSubmit.call(this,a):!t||(z.ajax({type:f.attr("method"),url:f.attr("action"),data:a.serialize(),success:function(e){"function"==typeof l&&l.call(this,e,a)}}),!1);e.preventDefault()}),s.on("focus",function(e){q(e.target)}),s.on("blur",function(e){o(e.target)}),r.on("click",function(e){n(e.target)})})}}(jQuery);