@progress/kendo-ui
Version:
This package is part of the [Kendo UI for jQuery](http://www.telerik.com/kendo-ui) suite.
3 lines (2 loc) • 13.6 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("kendo.core.js")):"function"==typeof define&&define.amd?define(["exports","kendo.core.min"],e):e(((t="undefined"!=typeof globalThis?globalThis:t||self).kendo=t.kendo||{},t.kendo._globals=t.kendo._globals||{},t.kendo._globals.Validator={}))}(this,(function(t){!function(t){let e=window.kendo,a=e.ui.Widget,i=".kendoValidator",n="k-invalid-msg",r=new RegExp(n,"i"),o="k-invalid",s="k-valid",l="k-validation-summary",d="k-text-error",u="k-messagebox k-messagebox-error",c=".k-upload-button-wrap",h="aria-invalid",m=/^[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])?)*$/i,p=["http:","https:","ftp:","ftps:"],f=":input:not(:button,[type=submit],[type=reset],[disabled],[readonly])",g=":checkbox:not([disabled],[readonly])",v="[type=number],[type=range]",_="blur",y="name",k="form",b="novalidate",S="validate",x="change",w="validateInput",C=function(t,e){return"string"==typeof e&&(e=new RegExp("^(?:"+e+")$")),e.test(t)},E=function(t,e){return!!t.length&&null!=t[0].attributes[e]};function I(e){return t.parseHTML?t(t.parseHTML(e)):t(e)}function A(a,i){for(var n,o=t(),s=0,l=a.length;s<l;s++)n=a[s],r.test(n.className)&&n.getAttribute(e.attr("for"))===i&&(o=o.add(n));return o}function M(t,e){return!!t&&("string"==typeof t.nodeName&&"LABEL"===t.nodeName&&("string"==typeof t.getAttribute("for")&&"string"==typeof e.getAttribute("id")&&t.getAttribute("for")===e.getAttribute("id")))}e.ui.validator||(e.ui.validator={rules:{},messages:{},allowSubmit:t.noop,validateOnInit:t.noop});var V=({errors:t})=>{let e="<ul>";for(var a=0;a<t.length;a+=1)e+=`<li><a data-field="${t[a].field}" href="#">${t[a].message}</a></li>`;return e+="</ul>",e},T=a.extend({init:function(i,n){var r=this,o=function(a){var i,n=e.ui.validator.ruleResolvers||{},r={};for(i in n)t.extend(!0,r,n[i].resolve(a));return r}(i),s="["+e.attr("validate")+"!=false]";(n=n||{}).rules=t.extend({},e.ui.validator.rules,o.rules,n.rules),n.messages=t.extend({},e.ui.validator.messages,o.messages,n.messages),a.fn.init.call(r,i,n),r._errorTemplate=e.template(r.options.errorTemplate),r._summaryTemplate=e.template(r.options.validationSummary.template||V),r.element.is(k)&&r.element.attr(b,b);const l=r.element.attr("id");r._shouldSearchDocument=r.element.is(k)&&null!=l&&t(`:input[form='${l}']`).length>0,r._containerElement=r._shouldSearchDocument?t(document):r.element,r._inputSelector=r._buildSelector(f,s),r._checkboxSelector=r._buildSelector(g,s),r._errors={},r._attachEvents(),r._isValidated=!1,r._validateOnInit()&&r.validate()},events:[S,x,w],options:{name:"Validator",errorTemplate:({message:t})=>`<span class="k-form-error">${t}</span>`,messages:{required:"{0} is required",pattern:"{0} is not valid",min:"{0} should be greater than or equal to {1}",max:"{0} should be smaller than or equal to {1}",step:"{0} is not valid",email:"{0} is not valid email",url:"{0} is not valid URL",date:"{0} is not valid date",dateCompare:"End date should be greater than or equal to the start date",captcha:"The text you entered doesn't match the image."},rules:{required:function(t){let e=this._containerElement,a=!t.attr("name")&&!t.is(":checked"),i=t.attr("name"),n=i&&i.indexOf("'")>-1?'"':"'",r=t.attr("name")&&!e.find("input[name="+n+t.attr("name")+n+"]:checked").length,o=t.filter("[type=checkbox]").length&&(a||r),s=t.filter("[type=file]").parents(c).children("input").length,l=t.filter("[type=radio]").length&&!e.find("input[name="+n+t.attr("name")+n+"]:checked").length,d=t.val();if(s){let e=!1;return t.filter("[type=file]").parents(c).children("input").each((function(a){t.val()&&(e=!0)})),!(E(t,"required")&&!e)}return!(E(t,"required")&&(!d||""===d||0===d.length||o||l))},pattern:function(t){return!t.filter("[type=text],[type=email],[type=url],[type=tel],[type=search],[type=password]").filter("[pattern]").length||""===t.val()||C(t.val(),t.attr("pattern"))},min:function(t){return!t.filter(v+",["+e.attr("type")+"=number]").filter("[min]").length||""===t.val()||(parseFloat(t.attr("min"))||0)<=e.parseFloat(t.val())},max:function(t){return!t.filter(v+",["+e.attr("type")+"=number]").filter("[max]").length||""===t.val()||(parseFloat(t.attr("max"))||0)>=e.parseFloat(t.val())},step:function(t){if(t.filter(v+",["+e.attr("type")+"=number]").filter("[step]").length&&""!==t.val()){var a,i=parseFloat(t.attr("min"))||0,n=e.parseFloat(t.attr("step"))||1,r=parseFloat(t.val()),o=(s=((s=n)+"").split(".")).length>1?s[1].length:0;return o?(a=Math.pow(10,o),Math.floor((r-i)*a)%(n*a)/Math.pow(100,o)==0):(r-i)%n==0}var s;return!0},email:function(t){return function(t,e,a){var i=t.val();return!t.filter(e).length||""===i||C(i,a)}(t,"[type=email],["+e.attr("type")+"=email]",m)},url:function(t){if(t.filter("[type=url],["+e.attr("type")+"=url]").length&&""!==t.val())try{const e=new URL(t.val());return p.includes(e.protocol)}catch{return!1}return!0},date:function(t){return!t.filter("[type^=date],["+e.attr("type")+"=date]").length||""===t.val()||null!==e.parseDate(t.val(),t.attr(e.attr("format")))},captcha:function(t){if(t.filter("["+e.attr("role")+"=captcha]").length){var a=this,i=e.widgetInstance(t),n=function(t){return null!=t};if(t.data("captcha_validating")||n(i.isValid())||!i.getCaptchaId()||(t.data("captcha_validating",!0),a._validating=!0,i.validate().done((function(){a._validating=!1,a._checkElement(t)})).fail((function(t){a._validating=!1,t.error&&"handler_not_defined"===t.error&&window.console.warn("Captcha's validationHandler is not defined! You should either define a proper validation endpoint or declare a callback function to ensure the required behavior.")}))),n(i.isValid()))return t.removeData("captcha_validating"),i.isValid()}return!0}},validateOnBlur:!0,validationSummary:!1},_buildSelector:function(t,e){if(!this._shouldSearchDocument)return t+e;const a=this.element.attr("id");return`#${a} `+t+e+`,[form="${a}"]`},_allowSubmit:function(){return e.ui.validator.allowSubmit(this.element,this.errors())},_validateOnInit:function(){return e.ui.validator.validateOnInit(this.element)},destroy:function(){a.fn.destroy.call(this),this.element.off(i),this.validationSummary&&(this.validationSummary.off(i),this.validationSummary=null)},value:function(){return!!this._isValidated&&0===this.errors().length},_submit:function(t){return!(!this.validate()&&!this._allowSubmit()||this._validating)||(t.stopPropagation(),t.stopImmediatePropagation(),t.preventDefault(),!1)},_checkElement:function(t){var e=this.value();this.validateInput(t),this.value()!==e&&this.trigger(x)},_attachEvents:function(){const e=this,a=e._containerElement;e.element.is(k)&&e.element.on("submit"+i,e._submit.bind(e)),e.options.validateOnBlur&&(a.is(f)?(a.on(_+i,(function(){e._checkElement(e.element)})),a.is(g)&&a.on("click"+i,(function(){e._checkElement(e.element)}))):(a.on(_+i,e._inputSelector,(function(){e._checkElement(t(this))})),a.on("click"+i,e._checkboxSelector,(function(){e._checkElement(t(this))}))))},validate:function(){let e,a,i,n=!1,r=this._containerElement,o=this.value();if(this._errors={},this.element.is(f))n=this.validateInput(this.element);else{let o=!1;for(e=r.find(this._inputSelector),r.find(c).each((function(){let a=t(this).find("input[type='file']").first();a.length&&Array.prototype.push.call(e,a[0])})),a=0,i=e.length;a<i;a++)this.validateInput(e.eq(a))||(o=!0);n=!o}return this.options.validationSummary&&!o&&this.showValidationSummary(),this.trigger(S,{valid:n,errors:this.errors()}),o!==n&&this.trigger(x),n},validateInput:function(a){if(a=t(a),!e.isEmpty(a.closest(".k-otp"))){let t=a.closest(".k-otp");a=t.find("input[data-role='otpinput']")}this._isValidated=!0;const i=this,r=i._errorTemplate,l=i._checkValidity(a),u=l.valid,c="."+n,m=a.attr(y)||"",p=i._findMessageContainer(m).add(a.next(c).filter((function(){let a=i._shouldSearchDocument?t(document):t(this);return!a.filter("["+e.attr("for")+"]").length||a.attr(e.attr("for"))===m}))).addClass("k-hidden"),f=u?"":i._extractMessage(a,l.key),g=u?"":I(r({message:(b=f,b.replace(/&/g,"&").replace(/"/g,'"').replace(/'/g,"'").replace(/</g,"<").replace(/>/g,">")),field:m})),v=!a.attr(h),_=a.is(".k-input-inner"),k=a.parent(".k-input");var b;let S;if(a.removeAttr(h),!a.hasClass("k-hidden")||"otpinput"!=a.attr("data-role")&&"upload"!=a.attr("data-role")||(S=e.widgetInstance(a)),a.hasClass("k-hidden")&&"signature"==a.attr("data-role")&&(S=e.widgetInstance(a.closest(".k-signature"))),a.is("[type=radio]")&&(S=e.widgetInstance(a.closest(".k-radio-list"))),a.is("[type=checkbox]")&&(S=e.widgetInstance(a.closest(".k-checkbox-list"))),u||a.data("captcha_validating"))delete i._errors[m];else{i._errors[m]=f;let t=p.attr("id");if(i._decorateMessageContainer(g,m),t&&g.attr("id",t),0!==p.length)p.replaceWith(g);else{S=S||e.widgetInstance(a);let t=a.parent().get(0),i=a.next().get(0),n=a.prev().get(0);!S&&a.is("[type=radio]")&&(S=e.widgetInstance(a.closest(".k-radio-list"))),!S&&a.is("[type=checkbox]")&&(S=e.widgetInstance(a.closest(".k-checkbox-list"))),S&&S.wrapper&&(S.element!==S.wrapper||["Signature","RadioGroup","CheckBoxGroup"].indexOf(S.options.name)>-1)?g.insertAfter(S.wrapper):t&&"LABEL"===t.nodeName?g.insertAfter(t):i&&M(i,a[0])?g.insertAfter(i):n&&M(n,a[0])?g.insertAfter(a):_&&k.length?g.insertAfter(k):g.insertAfter(a)}g.removeClass("k-hidden"),a.attr(h,!0)}if(v!==u&&this.trigger(w,{valid:u,input:a,error:f,field:m}),S=S&&"Signature"==S.options.name?S:e.widgetInstance(a),(!S||!S._inputWrapper&&!S.wrapper||a.is("[type=checkbox]")||a.is("[type=radio]"))&&(a.toggleClass(o,!u),a.toggleClass(s,u)),S){let t=S.options.name,e=S._inputWrapper||S.wrapper,n=S._inputLabel;"OTPInput"==t?u?(S._removeInvalidState.bind(i),S._removeInvalidState(e,!0)):(S._addInvalidState.bind(i),S._addInvalidState(e,!0)):!e||a.is("[type=checkbox]")||a.is("[type=radio]")||(e.toggleClass(o,!u),e.toggleClass(s,u)),n&&n.toggleClass(d,!u)}if(v!==u){let t=g?g.attr("id"):p.attr("id");i._associateMessageContainer(a,t),this.options.validationSummary&&this.options.validateOnBlur&&this.showValidationSummary()}return u},hideMessages:function(){const t="."+n,e=this._containerElement;this._disassociateMessageContainers(),e.is(f)?e.next(t).addClass("k-hidden"):e.find(t).addClass("k-hidden")},reset:function(){const t=this,e=t._containerElement,a=e.find("."+o),i=e.find("."+d);t._errors=[],t.hideMessages(),t.hideValidationSummary(),a.removeAttr(h),a.removeClass(o),i.removeClass(d)},_findMessageContainer:function(a){let i,n=e.ui.validator.messageLocators,r=t();for(let t=0,e=this.element.length;t<e;t++){let e=this._shouldSearchDocument?document:this.element[t];r=r.add(A(e.getElementsByTagName("*"),a))}for(i in n)r=r.add(n[i].locate(this._containerElement,a));return r},_decorateMessageContainer:function(t,a){var i,r=e.ui.validator.messageLocators;for(i in t.addClass(n).attr(e.attr("for"),a||""),t.attr("id")||t.attr("id",a+"-error"),r)r[i].decorate(t,a)},_extractMessage:function(t,a){var i,n=this.options.messages[a],r=t.attr(y);e.ui.Validator.prototype.options.messages[a]||(i=e.isFunction(n)?n(t):n),n=e.isFunction(n)?n(t):n;const o=t.parent().closest("[name='"+r+"']"),s=o.length?o:t;return e.format(s.attr(e.attr(a+"-msg"))||s.attr("validationMessage")||i||n||s.attr("title")||"",r,s.attr(a)||t.attr(e.attr(a)))},_checkValidity:function(t){var e,a=this.options.rules;for(e in a)if(!a[e].call(this,t))return{valid:!1,key:e};return{valid:!0}},errors:function(){var t,e=[],a=this._errors;for(t in a)e.push(a[t]);return e},setOptions:function(t){t.validationSummary&&this.hideValidationSummary(),e.deepExtend(this.options,t),this.destroy(),this.init(this.element,this.options),this._setEvents(this.options)},_getInputNames:function(){const e=this._containerElement.find(this._inputSelector);let a=[];for(let i=0,n=e.length;i<n;i++){let n=t(e[i]);E(n,y)&&(-1===a.indexOf(n.attr(y))||0===n.closest(".k-checkbox-list").length&&0===n.closest(".k-radio-list").length)&&a.push(n.attr(y))}return a},_associateMessageContainer:function(t,a){var i=e.getWidgetFocusableElement(t);i&&a&&e.toggleAttribute(i,"aria-describedby",a)},_disassociateMessageContainers:function(){for(var e,a,i=this,r=i.element.find("."+o).addBack(),s=0;s<r.length;s+=1)(e=t(r[s])).is("input")&&(a=i._findMessageContainer(e.attr(y)).add(e.next("."+n)).attr("id"),i._associateMessageContainer(e,a))},_errorsByName:function(){for(var t=this,e=t._getInputNames(),a=[],i=0;i<e.length;i+=1){var n=e[i];t._errors[n]&&a.push({field:n,message:t._errors[n]})}return a},_renderSummary:function(){var e,a=this,n=this.options.validationSummary,r=this.element.prev();return(e=n.container?t(n.container):r&&r.hasClass(l)?r:t("<div />").insertBefore(a.element)).addClass([l,u].join(" ")),e.attr("role","alert"),e.on("click"+i,a._summaryClick.bind(a)),e},_summaryClick:function(a){a.preventDefault();var i,n=t(a.target),r=this.element.find("[name='"+n.data("field")+"']");r.length&&(i=e.getWidgetFocusableElement(r))&&i.trigger("focus")},showValidationSummary:function(){var t,e=this,a=e.validationSummary,i=e._errorsByName();a||(a=e.validationSummary=e._renderSummary()),t=I(e._summaryTemplate({errors:i})),a.html(t),a.toggleClass("k-hidden",!i.length)},hideValidationSummary:function(){var t=this.validationSummary;t&&t.addClass("k-hidden")}});e.ui.plugin(T)}(window.kendo.jQuery);var e=kendo;t.__meta__={id:"validator",name:"Validator",category:"web",description:"The Validator offers an easy way to do a client-side form validation.",depends:["core"]},t.default=e,Object.defineProperty(t,"__esModule",{value:!0})}));
//# sourceMappingURL=kendo.validator.min.js.map