validate.js 3.04 KB
var JFormValidator=function(){"use strict";var t,e,a,r=function(e,a,r){r=""===r||r,t[e]={enabled:r,exec:a}},n=function(t,e){var a,r,n,i,l=e.data("label");void 0===l&&(a=e.attr("id"),r=e.get(0).form,i=jQuery(r),l=!!a&&((n=i.find("#"+a+"-lbl")).length?n:!!(n=i.find('label[for="'+a+'"]')).length&&n),e.data("label",l)),!1===t?(e.addClass("invalid").attr("aria-invalid","true"),l&&l.addClass("invalid")):(e.removeClass("invalid").attr("aria-invalid","false"),l&&l.removeClass("invalid"))},i=function(e){var a,r=jQuery(e);if(r.attr("disabled"))return n(!0,r),!0;if(r.attr("required")||r.hasClass("required"))if("fieldset"===r.prop("tagName").toLowerCase()&&(r.hasClass("radio")||r.hasClass("checkboxes"))){if(!r.find("input:checked").length)return n(!1,r),!1}else if(!r.val()||r.hasClass("placeholder")||"checkbox"===r.attr("type")&&!r.is(":checked"))return n(!1,r),!1;return a=r.attr("class")&&r.attr("class").match(/validate-([a-zA-Z0-9\_\-]+)/)?r.attr("class").match(/validate-([a-zA-Z0-9\_\-]+)/)[1]:"",r.attr("pattern")&&""!=r.attr("pattern")?r.val().length?(l=new RegExp("^"+r.attr("pattern")+"$").test(r.val()),n(l,r),l):r.attr("required")||r.hasClass("required")?(n(!1,r),!1):(n(!0,r),!0):""===a?(n(!0,r),!0):a&&"none"!==a&&t[a]&&r.val()&&!0!==t[a].exec(r.val(),r)?(n(!1,r),!1):(n(!0,r),!0)},l=function(t){var e,r,n,l,s,u,o=!0,d=[];for(s=0,u=(e=jQuery(t).find("input, textarea, select, fieldset")).length;s<u;s++)jQuery(e[s]).hasClass("novalidate")||!1===i(e[s])&&(o=!1,d.push(e[s]));if(jQuery.each(a,function(t,e){!0!==e.exec()&&(o=!1)}),!o&&d.length>0){for(r=Joomla.JText._("JLIB_FORM_FIELD_INVALID"),n={error:[]},s=d.length-1;s>=0;s--)(l=jQuery(d[s]).data("label"))&&n.error.push(r+l.text().replace("*",""));Joomla.renderMessages(n)}return o},s=function(t){for(var a,r=[],n=jQuery(t),s=0,u=(a=n.find("input, textarea, select, fieldset, button")).length;s<u;s++){var o=jQuery(a[s]),d=o.prop("tagName").toLowerCase();"input"!==d&&"button"!==d||"submit"!==o.attr("type")&&"image"!==o.attr("type")?"button"===d||"input"===d&&"button"===o.attr("type")||(o.hasClass("required")&&o.attr("aria-required","true").attr("required","required"),"fieldset"!==d&&(o.on("blur",function(){return i(this)}),o.hasClass("validate-email")&&e&&a[s].setAttribute("type","email")),r.push(o)):o.hasClass("validate")&&o.on("click",function(){return l(t)})}n.data("inputfields",r)};return function(){var n;t={},a=a||{},(n=document.createElement("input")).setAttribute("type","email"),e="text"!==n.type,r("username",function(t,e){return!new RegExp("[<|>|\"|'|%|;|(|)|&]","i").test(t)}),r("password",function(t,e){return/^\S[\S ]{2,98}\S$/.test(t)}),r("numeric",function(t,e){return/^(\d|-)?(\d|,)*\.?\d*$/.test(t)}),r("email",function(t,e){return t=punycode.toASCII(t),/^[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])?)*$/.test(t)});for(var i=jQuery("form.form-validate"),l=0,u=i.length;l<u;l++)s(i[l])}(),{isValid:l,validate:i,setHandler:r,attachToForm:s,custom:a}};document.formvalidator=null,jQuery(function(){document.formvalidator=new JFormValidator});