-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.validateWrapper.min.js
11 lines (11 loc) · 7.38 KB
/
jquery.validateWrapper.min.js
1
2
3
4
5
6
7
8
9
10
11
/*!
* JQuery validateWrapper Plugin v1.19.2
* Works well for jQuery validate Plugin v1.19.2
* Copyright (c) 2019 Siddhant Naik
*
* Plugin Name: validateWrapper
* Description: This validateWrapper plugin will run above the JQuery Validation Plugin.
* Basically, the validateWrapper plugin will help you to validate form elements by adding a few lines of code.
* Just use the form selector and call the wrapper plugin and the form will start validating.
*/
!function(e){"use strict";let t;const s="validateWrapper";function r(t,r){this._element=t,this._pluginName=s,this._defaults=e.fn[s].defaults,void 0===r&&(r={}),this._settings=e.extend({},this._defaults,r),delete this._settings.messages,delete this._settings.groups,delete this._settings.require_from_group,delete this._settings.showErrors,void 0===r.messages&&(r.messages={}),jQuery.isEmptyObject(r.messages)||"object"==typeof r.messages||delete r.messages,void 0===r.groups&&(r.groups={}),jQuery.isEmptyObject(r.groups)||"object"==typeof r.groups||delete r.groups,void 0===r.require_from_group&&(r.require_from_group={}),jQuery.isEmptyObject(r.require_from_group)||"object"==typeof r.require_from_group||delete r.require_from_group,void 0===r.showErrors&&(r.showErrors={}),jQuery.isEmptyObject(r.showErrors)||"object"==typeof r.showErrors||delete r.showErrors,this._messages=e.extend({},this._defaults.messages,r.messages),this._groups=e.extend({},this._defaults.groups,r.groups),this._require_from_group=e.extend({},this._defaults.require_from_group,r.require_from_group),this._showErrors=e.extend({},this._defaults.showErrors,r.showErrors),this._init()}e.extend(r.prototype,{_init:function(){(t=this)._build(),t._validate()},_build:function(){t.$_element=e(t._element)},_bindEvents:function(s,r,n,i){e(s,t.$_element).on(r+"."+t._pluginName,function(){switch(i){case"reset":"reset"!=e(s,t.$_element).attr("type")&&"button"!=e(s,t.$_element).attr("type")||n.resetForm();break;case"validate":default:t.$_element.valid()&&(t._callback(t.$_element),t._settings.destroyOnCallback&&t._unbindEvents(s))}})},_unbindEvents:function(s){e(s,t.$_element).off("."+t._pluginName)},_destroy:function(e){e.destroy(),t.$_element.removeData()},_validate:function(){var s;let r={normalizer:"function"==typeof t._settings.normalizer?t._settings.normalizer:t._normalizer,highlight:"function"==typeof t._settings.highlight?t._settings.highlight:t._highlight,unhighlight:"function"==typeof t._settings.unhighlight?t._settings.unhighlight:t._unHighlight,invalidHandler:"function"==typeof t._settings.invalidHandler?t._settings.invalidHandler:t._inValidHandler,groups:"object"==typeof t._groups?t._groups:null,errorPlacement:"function"==typeof t._settings.errorPlacement?t._settings.errorPlacement:t._errorPlacement,submitHandler:function(e,r){t._callback(e,r),t._settings.destroyOnCallback&&t._destroy(s)}},n=e.extend({},t._settings,r);s=t.$_element.validate(n),t._additionalMethod(s)},_additionalMethod:function(s){jQuery.extend(jQuery.validator.messages,t._messages);for(let e in t._require_from_group)jQuery.validator.addClassRules(e,{require_from_group:[t._require_from_group[e],"."+e]});for(let s in t._require_from_group)for(let r in t._messages)s==r&&""!=t._messages[r]&&e("."+r).each(function(){void 0!==e('input[name^="'+this.name+'"]',t.$_element)?e('input[name^="'+this.name+'"]',t.$_element).each(function(){e("#"+this.id,t.$_element).rules("add",{messages:{require_from_group:t._messages[r]}})}):void 0!==e("[name="+this.name+"]",t.$_element)&&e("[name="+this.name+"]",t.$_element).rules("add",{messages:{require_from_group:t._messages[r]}})});for(let s in t._groups)for(let r in t._messages)s==r&&""!=t._messages[r]&&e("."+r).each(function(){void 0!==e('input[name^="'+this.name+'"]',t.$_element)?e('input[name^="'+this.name+'"]',t.$_element).each(function(){e("#"+this.id,t.$_element).rules("add",{messages:{required:t._messages[r]}})}):void 0!==e("[name="+this.name+"]",t.$_element)&&e("[name="+this.name+"]",t.$_element).rules("add",{messages:{required:t._messages[r]}})});jQuery.validator.addMethod("email",function(e){if(""==e)return!0;let t,s,r,n,i;return t=e.indexOf("@"),(r=e.substr(0,t)).lastIndexOf("_")!=r.length-1&&r.lastIndexOf(".")!=r.length-1&&r.lastIndexOf("-")!=r.length-1&&((i=(n=e.substr(t+1)).substr(0,n.indexOf("."))).lastIndexOf("-")!=i.length-1&&i.indexOf("-")==i.lastIndexOf("-")&&(s=/(^[a-zA-Z0-9]+[._-]{0,1})+([a-zA-Z0-9]+[_]{0,1})*?[a-zA-Z0-9]@([a-zA-Z0-9]+[-]{0,1})+(.[a-zA-Z0-9]+)*(\.[a-zA-Z]{2,})$/).test(e))}),jQuery.validator.addMethod("age",function(e){return/^\d{0,3}$/.test(e)}),jQuery.validator.addMethod("ckeditor_required",function(t,s){let r=e(s).attr("id"),n=CKEDITOR.instances[r];return 0===(t=n.getData().replace(/<[^>]*>/gi,"").trim()).length?e(s).val(t):e(s).val(n.getData()),e(s).val().length>0});let r=t._settings.addToValidator;"function"==typeof r&&r(t.$_element,s),t._settings.validateOnLoad&&s.form(),t._settings.validateOnClick&&t._bindEvents("._validate_oc","click"),t._settings.validateOnKeyPress&&t._bindEvents("._validate_kp","keypress"),t._settings.resetValidator&&t._bindEvents("._reset_validator","click",s,"reset"),s.showErrors(t._showErrors)},_normalizer:function(t){return e.trim(t)},_highlight:function(t,s,r){jQuery(t).next().hasClass("input-group-append")&&jQuery(t).next().children().addClass(s).removeClass(r),jQuery(t).prev().hasClass("input-group-prepend")&&jQuery(t).prev().children().addClass(s).removeClass(r),jQuery(t).is("input[type=radio]")?e("input[name='"+t.name+"']").addClass(s).removeClass(r):jQuery(t).addClass(s).removeClass(r)},_unHighlight:function(t,s,r){jQuery(t).next().hasClass("input-group-append")&&jQuery(t).next().children().removeClass(s).addClass(r),jQuery(t).prev().hasClass("input-group-prepend")&&jQuery(t).prev().children().removeClass(s).addClass(r),jQuery(t).is("input[type=radio]")?e("input[name='"+t.name+"']").removeClass(s).addClass(r):jQuery(t).removeClass(s).addClass(r)},_inValidHandler:function(t,s){s.numberOfInvalids()&&e("html, body").animate({scrollTop:e(s.errorList[0].element).offset().top-100},100)},_errorPlacement:function(s,r){if("true"==r.attr("hide-validation-message"))return!0;if(r.next().hasClass("input-group-append")||r.prev().hasClass("input-group-prepend")||r.hasClass("append-msg-to-parent"))s.insertAfter(r.parent());else if(r.is("input[type=checkbox]")||r.is("input[type=radio]"))r.closest(".form-check").parent().append(s);else if(r.hasClass("required-from-group"))for(let n in t._require_from_group){let t=e("."+n+":last");if(e(r).attr("id")==e(t).attr("id")){s.insertAfter(t.parent().parent());break}}else if(r.hasClass("group-in-one")){for(let n in t._groups)if(r.hasClass(n)){let t=e("."+n+":last");if(e(r).attr("id")==e(t).attr("id")){s.insertAfter(t.parent().parent());break}}}else s.insertAfter(r)},_callback:function(e,s=null){let r=t._settings.onComplete;"function"==typeof r&&r(e,s)}}),e.fn[s]=function(t){return this.each(function(){e.data(this,"plugin_"+s)||e.data(this,"plugin_"+s,new r(this,t))}),this},e.fn[s].defaults={ignore:":hidden:not(.hidden-required, .editor-required), .ignore",errorClass:"error",errorElement:"div",validClass:"success",groups:null,require_from_group:null,normalizer:null,validateOnLoad:!1,validateOnClick:!1,validateOnKeyPress:!1,resetValidator:!1,destroyOnCallback:!1,highlight:null,unhighlight:null,invalidHandler:null,errorPlacement:null,addToValidator:null,onComplete:null,messages:{require_from_group:jQuery.validator.format("Please fill out at least {0} of these fields."),age:"Please enter valid age."},showErrors:null}}(jQuery);