This repository has been archived by the owner on Nov 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.csssr.validation.min.js
1 lines (1 loc) · 12.9 KB
/
jquery.csssr.validation.min.js
1
!function(t){"function"==typeof define&&define.amd?define(["jquery","url"],t):"object"==typeof exports?module.exports=t(require("jquery"),require("url")):t(jQuery,URL)}(function(t,e){function a(e,a,i){var n=e.data("events")||t._data(e[0]).events,r=n[a],l=i?r.splice(r.delegateCount-1,1)[0]:r.pop();r.splice(i?0:r.delegateCount||0,0,l)}function i(e,i,n){var r=i.split(/\s+/);e.each(function(){for(var e=0;e<r.length;++e){var i=t.trim(r[e]).match(/[^\.]+/i)[0];a(t(this),i,n)}})}function n(e,a){this.element=e;var i=s.getDataOptions(e),n={};if(i.inheritValidationOptions){var l=t(e).parents("[data-validate], [data-validation-container]");l.length&&(n=s.getDataOptions(l.eq(0)))}this.options=t.extend(!0,{},t[r].defaults,t[r].globals,n,i,a),this.init()}t.fn.onFirst=function(e,a){var n=t(this),r="string"==typeof a;if(t.fn.on.apply(n,arguments),"object"==typeof e)for(var l in e)e.hasOwnProperty(l)&&i(n,l,r);else"string"==typeof e&&i(n,e,r);return n};var r="csssrValidation",l=["valid-class","invalid-class","valid-container-class","invalid-container-class","valid-textarea-class","invalid-textarea-class","valid-select-class","invalid-select-class","class-target","valid-class-target","invalid-class-target","valid-textarea-class-target","invalid-textarea-class-target","valid-select-class-target","invalid-select-class-target","required-selector","numeric-selector","inputmode-selector","allow-empty-selector","pattern-attribute","input-pattern-attribute","inputmode-attribute","minlength-attribute","maxlength-attribute","min-attribute","max-attribute","type-attribute","trim-attribute","remove-invalid-class-on","validate-fields-on","silent-validation-on","msg-target","empty-value-msg","invalid-value-msg","empty-msg-target","invalid-msg-target","invalid-length-msg","invalid-length-msg-target","max-validation-level","inherit-validation-options","trigger-field-events","data-element-selector"];t[r]=t[r]||{},t[r].defaults={requiredSelector:"[required], .js-required",requiredPreCheck:!1,numericSelector:'[inputmode="numeric"]',inputmodeSelector:"input[inputmode], input[data-input-pattern], textarea[data-input-pattern]",allowEmptySelector:"[data-allow-empty]",minMaxSelector:"input[min], input[max]",patternAttribute:"pattern",inputPatternAttribute:"data-input-pattern",inputmodeAttribute:"inputmode",minlengthAttribute:"minlength",maxlengthAttribute:"maxlength",minAttribute:"min",maxAttribute:"max",typeAttribute:"type",trimAttribute:"data-trim",msgTarget:!1,emptyValueMsg:"",emptyMsgTarget:!1,invalidValueMsg:"",invalidMsgTarget:!1,removeInvalidClassOn:"focus",validateFieldsOn:!1,silentValidationOn:!1,maxValidationLevel:Number.MAX_VALUE,inheritValidationOptions:!1,triggerFieldEvents:!1,elementSelector:"input, textarea, select",masks:{numbered:{has:function(t){return t.length&&"undefined"!=typeof t[0].numbered},valid:function(t,e){var a=this.has(t)&&new Numbered(t).validate();return a>0||e&&0===a},init:function(t){new Numbered(t)}},inputmask:{selector:"[data-inputmask]",has:function(t){return t.inputmask("getemptymask").length},valid:function(t){return t.inputmask("isComplete")},init:function(t){t.inputmask()}}},patterns:{email:/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i,url:function(t){try{var a=new e(t);return"https:"===a.protocol||"http:"===a.protocol}catch(i){return!1}}},inputPatterns:{cyrillic:/[а-яёії\s]/i,latin:/[a-z\s]/i,numeric:/[0-9]/,letters:/[a-zа-яёії\s]/i,email:/[a-zа-яёії0-9\.\-_@]/i,phone:/[\+0-9]/}};var s={capitalize:function(t){return t.charAt(0).toUpperCase()+t.slice(1)},_onSubmit:function(e){var a=t.data(e.target,r),i=s.validate.apply(a,[]),n=t.extend(!0,{},a.options,t[r].globals||{},s.getDataOptions(a.element));return i&&"validate"!==e.type||e.stopImmediatePropagation(),a.element.trigger(i?"valid":"invalid"),n.validContainerClass&&a.element.toggleClass(n.validContainerClass,i),n.invalidContainerClass&&a.element.toggleClass(n.invalidContainerClass,!i),i},_onSilentValidate:function(){var e=t(this),a=e.closest("form, [data-validation-container]").data(r);a.element.trigger(s.validate.apply(a,[!1,!0])?"valid":"invalid")},_onValidateField:function(){var e=t(this),a=e.closest("form, [data-validation-container]").data(r);s.validate.apply(a,[e])},_onRemoveInvalidClass:function(){var e=t(this),a=e.closest("form, [data-validation-container]").data(r);s.toggleClass(e,!1,s.getClass.apply(a,[e,"invalid"]),s.getClassTarget.apply(a,[e,"invalid"]))},_onBlur:function(e){var a=t(this),i=a.closest("form, [data-validation-container]").data(r),n=a.filter(i.options.numericSelector).length,l=a.filter(i.options.requiredSelector).length,o=a.attr(i.options.minAttribute),u=a.attr(i.options.maxAttribute),d=a.attr(i.options.trimAttribute)||!1,c=a.data("jcv-last-char")||!1,g=s.trim(a.val()||"",d);d&&("blur"===e.type||"focusout"===e.type||"right"!==d&&" "===c&&!g)&&a.val(g),a.data("jcv-last-char",null),n&&(l||a.val())&&(o>Number(a.val())?a.val(o):u<Number(a.val())&&a.val(u))},isSpecialKey:function(e,a){return-1!==t.inArray(e,[46,8,9,27,13,110])||65===e&&a===!0||e>=35&&39>=e?!0:!1},trim:function(t,e){return e?t="left"===e?t.replace(/^\s+/,""):"right"===e?t.replace(/\s+$/,""):t.replace(/^\s+|\s+$/g,""):t},_onKeyDown:function(e){var a=t(this),i=a.closest("form, [data-validation-container]").data(r),n=a.filter(i.options.numericSelector).length;n&&!s.isSpecialKey(e.keyCode,e.ctrlKey)&&(e.shiftKey||e.keyCode<48||e.keyCode>57)&&(e.keyCode<96||e.keyCode>105)&&e.preventDefault()},_onKeyPress:function(e){var a=t(this),i=a.closest("form, [data-validation-container]").data(r),n=s.getMask(a,i.options.masks),l=n?!1:s.getPattern.apply(i,[a,i.options.inputmodeAttribute,i.options.inputPatternAttribute,i.options.inputPatterns]),o=a.attr(i.options.maxlengthAttribute)||Number.POSITIVE_INFINITY,u=e.key||String.fromCharCode(e.which||e.keyCode),d=a.attr(i.options.trimAttribute)||!1,c=s.trim(a.val(),d);if(13!==e.keyCode)return" "===u&&d&&"right"!==d&&""===c?!1:(a.data("jcv-last-char",u),a.val().length<=o&&(!l||s.matchesPatterns(u,l,a)))},_onPaste:function(){var e=t(this),a=e.closest("form, [data-validation-container]").data(r),i=s.getMask(e,a.options.masks),n=e.attr(a.options.trimAttribute)||!1,l=i?!1:s.getPattern.apply(a,[e,a.options.inputmodeAttribute,a.options.inputPatternAttribute,a.options.inputPatterns]);setTimeout(function(){if(l){var a=s.trim(e.val()||"",n),i="";i=t.map(a.split(""),function(t){return s.matchesPatterns(t,l,e)?t:""}).join(""),i=s.trim(i||"",n),e.val(i)}},100)},getDataOptions:function(e){var a={};return t.each(l,function(i,n){var r=e.data(n);void 0!==r&&(a[t.camelCase(n)]=r)}),a},getMask:function(e,a){for(var i in a)if(a.hasOwnProperty(i)&&void 0!==t.fn[i]&&(mask=a[i]).has(e))return mask;return!1},getPattern:function(e,a,i,n){var r=e.attr(a),l=e.attr(i),s=e.attr(i+"-flags"),o=[],u=r?r.split(","):[];if(l){var d="!"===l[0];l=d?l.slice(1):l,o.push(d?function(t){return d===(null===t.match(new RegExp(l,s)))}:new RegExp(l,s))}return t.each(u,function(e,a){var i="!"===a[0];a=i?a.slice(1):a,void 0!==n[a]&&(t.isFunction(n[a])?o.push(i?function(t){return!n[a](t)}:n[a]):o.push(i?function(t){return i===(null===t.match(new RegExp(n[a],s)))}:new RegExp(n[a],s)))}),o},getTarget:function(e,a){if(a&&-1!==a.indexOf("/")){for(var i=a.split("/"),n=e,r=0;r<i.length;r+=2)n=r+1<i.length&&i[r+1]?n[i[r]](i[r+1]):n[i[r]]();return n}return t(a)},toggleClass:function(t,e,a,i,n,r,l){if(a){var o=t;i&&(o=s.getTarget(t,i));var u=o.data("jcv-timestamp"),d=o.data("jcv-vld-state");d=(null===d?!0:d)&&r,n?u!==n?o.data("jcv-timestamp",n).data("jcv-vld-state",r).toggleClass(a,e):o.data("jcv-vld-state",d).toggleClass(a,d===l):o.toggleClass(a,e)}},matchesPatterns:function(e,a,i){var n=!0;return t.each(a,function(a,r){n=n&&(t.isFunction(r)?r(e,i):null!==e.match(r))}),n},getClass:function(e,a){var i=s.getDataOptions(e),n=t.extend(!0,{},this.options,t[r].globals||{});return i[a+"Class"]||n[a+s.capitalize(e[0].tagName.toLowerCase())+"Class"]||n[a+"Class"]},getClassTarget:function(e,a){var i=s.getDataOptions(e),n=t.extend(!0,{},this.options,t[r].globals||{});return i[a+"ClassTarget"]||i.classTarget||n[a+s.capitalize(e[0].tagName.toLowerCase())+"ClassTarget"]||n[a+"ClassTarget"]||n.classTarget},validate:function(e,a){var i=this,n=t.extend(!0,{},i.options,t[r].globals||{}),l=t.grep([n.requiredSelector||"",n.numericSelector||"",n.minMaxSelector||"",n.inputmodeSelector||""],function(t){return""!==t}).join(","),o=!0,u=(new Date).getTime();return e=e?e.filter(l):i.element.find(l),n.requiredPreCheck&&(e=e.filter(n.requiredPreCheck)),e.each(function(){var e=t(this),r=s.getDataOptions(e);if("undefined"!=typeof r.maxValidationLevel&&Number(r.maxValidationLevel)<Number.MAX_VALUE){var l=e.parents("[data-validate], [data-validation-container]").filter(function(t,e){return r.maxValidationLevel>=t&&e===i.element[0]}).length;if(!l)return!0}var d=e.attr(n.trimAttribute)||!1;d&&e.val(s.trim(e.val()||"",d));var c=s.getMask(e,n.masks),g=c?!1:s.getPattern.apply(i,[e,"type",n.patternAttribute,n.patterns]),v=e.filter(n.allowEmptySelector).length||!e.filter(n.requiredSelector).length,m=e.attr(n.maxlengthAttribute)||Number.POSITIVE_INFINITY,p=e.attr(n.minlengthAttribute)||0,F=(e.val()||"").length,f=e.filter(n.numericSelector).length,h=e.attr(n.minAttribute),b=e.attr(n.maxAttribute),y=e.data("equal-to"),x="checkbox"===(e.attr(n.typeAttribute)||"").toLowerCase(),C=!e.val(),D=F>=Number(p)&&F<=Number(m),A=x?e.length&&e[0].checked:(v&&C||c&&c.valid(e,v)||g.length&&s.matchesPatterns(e.val(),g,e)||!c&&!g.length&&F>0)&&D&&(!f||(void 0===h||Number(h)<=Number(e.val()))&&(void 0===b||Number(b)>=Number(e.val())));if(A&&y){var S=s.getTarget(e,y);A=S.val()===e.val()}if((n.triggerFieldEvents||r.triggerFieldEvents)&&e.trigger(A?"valid":"invalid",a),!a){s.toggleClass(e,!A,s.getClass.apply(i,[e,"invalid"]),s.getClassTarget.apply(i,[e,"invalid"]),u,A,!1);var T=r.emptyValueMsg||n.emptyValueMsg||!1,k=r.emptyMsgTarget||r.msgTarget||n.emptyMsgTarget||n.msgTarget||!1,V=r.invalidValueMsg||n.invalidValueMsg||!1,P=r.invalidMsgTarget||r.msgTarget||n.invalidMsgTarget||n.msgTarget||!1,_=r.invalidLengthMsg||n.invalidLengthMsg||!1,E=r.invalidLengthMsgTarget||r.msgTarget||n.invalidLengthMsgTarget||n.msgTarget||!1;T&&k&&s.getTarget(e,k).text(C?T:""),C||(_&&E&&s.getTarget(e,E).text(D?"":_),D&&V&&P&&s.getTarget(e,P).text(A?"":V)),s.toggleClass(e,A,s.getClass.apply(i,[e,"valid"]),s.getClassTarget.apply(i,[e,"valid"]),u,A,!0)}o=o&&A}),o}};n.prototype={init:function(){var e=this;e.element.on("keydown",e.options.numericSelector,s._onKeyDown),e.element.on("keypress",e.options.requiredSelector,s._onKeyPress),e.element.on("paste",e.options.requiredSelector,s._onPaste),e.element.on("blur input",e.options.requiredSelector,s._onBlur),e.element.attr("novalidate",!0).onFirst("submit validate.csssr",s._onSubmit),e.options.removeInvalidClassOn&&e.element.on(e.options.removeInvalidClassOn,e.options.elementSelector,s._onRemoveInvalidClass),e.options.validateFieldsOn&&e.element.on(e.options.validateFieldsOn,e.options.elementSelector,s._onValidateField),e.options.silentValidationOn&&e.element.on(e.options.silentValidationOn,e.options.elementSelector,s._onSilentValidate),t.each(e.options.masks,function(a,i){i.selector&&void 0!==t.fn[a]&&i.init(e.element.find(i.selector))})},enableSilentValidation:function(t,e){var a=this;e=e||a.options.elementSelector,a.element.on(t,e,s._onSilentValidate)},destroy:function(){this.element.off("submit."+r+" validate.csssr",s._onSubmit).removeData(r)}},t.fn[r]=function(e){var a=arguments;return this.each(function(){var i=t.data(this,r);if(i)return e&&e.substring&&i[e].apply(i,[].splice.call(a,1)),!0;if(e&&e.substring)throw new Error(r+" not available for this DOM element!");return i=t(this),i.data(r,new n(i,e)),!0})},t(function(){t("body").onFirst("submit","form[data-validate]:not([novalidate])",function(e){e.preventDefault(),e.stopImmediatePropagation(),t(this).csssrValidation().submit()}),t("body").onFirst("validate.csssr","[data-validation-container]:not([novalidate])",function(e){var a=t(this);return a.data(r)?!0:(e.preventDefault(),e.stopImmediatePropagation(),a.csssrValidation().trigger("validate.csssr"),void 0)}),t("body").on("click","[data-validation-trigger]",function(){t(this).closest("[data-validation-container]").trigger("validate.csssr")}),t("form[data-validate]:not([novalidate]), [data-validation-container]:not([novalidate])").csssrValidation()})});