UNPKG

html-form-validation

Version:
1 lines 6.39 kB
!function(e){function t(t,i){return this.$form=e(t),this.options=e.extend(!0,{modal:!1,fieldsSelector:".form-input",removeErrorOnFocusOut:!1,ajax:{},lang:"en",beforeValidation:null,afterValidation:null,onValid:null},i),this.lang={ru:{emptyField:"Заполните поле",incorrectPhone:"Введите корректный номер",incorrectEmail:"Введите корректный Email",incorrectSelect:"Выберите один из вариантов",symbols:"символов",reqFieldLength:"Необходимая длинна поля - ",maxFieldLength:"Максимальная длинна поля - ",minFieldLength:"Минимальная длинна поля - ",minMaxFieldLength:{first:"Кол-во символов должно быть не более ",second:" и не менее "},notEqual:"Значение поля не совпало с ожидаемым"},en:{emptyField:"Fill in the field, please",incorrectPhone:"Please, enter a valid number",incorrectEmail:"Please, enter a valid email",incorrectSelect:"Please, select an option",symbol:"symbols",reqFieldLength:"Required field length is - ",maxFieldLength:"Maximum field length is - ",minFieldLength:"Minimum field length is - ",minMaxFieldLength:{first:"Number of characters should be less than ",second:" but not less than "},notEqual:"The field value did not match with the expected value"}},this.init(),this}var i={incorrectFields:".incorrect",error:".error",incorrect:"incorrect",formIsValid:"validated",dataType:"validation-type",textDataName:"validation-text",dataCondition:"validation-condition",textType:"text",phoneType:"phone",emailType:"email",checkboxType:"checkbox",radioType:"radio",selectType:"select",length:"length",equal:"equal",requiredToValidate:"required"};t.prototype.checkAndRunCallback=function(e){return"function"==typeof e?e(this):void 0!=e&&console.warn("Callback should be a function."),this},t.prototype.formIsValid=function(){return!this.$form.find(i.incorrectFields).length&&this.$form.hasClass(i.formIsValid)},t.validateEmail=function(e){return new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i).test(e)},t.prototype.removeIncorrectState=function(){return this.$form.find(this.options.fieldsSelector).removeClass(i.incorrect),this},t.prototype.resetForm=function(){return this.$form[0].reset(),this},t.prototype.removeIncorrectStateOnModalClose=function(){var e=this,t=e.$form.parents(".modal");return t.length?(t.on("hidden.bs.modal",function(){e.removeIncorrectState()}),this):(console.warn("No modal was found"),this)},t.prototype.removeIncorrectStateOnFocusOut=function(){var t=this;return e("body").on("click tap",function(i){e(i.target).closest(t.$form).length>0||t.removeIncorrectState()}),this},t.prototype.throwError=function(e,t){var n=e.find(i.error);e.addClass(i.incorrect),n.text(t)},t.prototype.checkFieldValidness=function(e,t,n,o){var a=e.data(i.textDataName),r=this.lang[this.options.lang];a&&a.length&&(n=a),o?t?this.$form.addClass(i.formIsValid):this.throwError(e,n):this.throwError(e,r.emptyField)},t.prototype.validateField=function(e){var t=this.lang[this.options.lang],n=e.data(i.dataType),o={condition:!0,errorText:t.emptyField,length:1};switch(n){case i.textType:o=this.validateTextField(e);break;case i.phoneType:o=this.validateTextField(e),o.errorText=t.incorrectPhone;break;case i.emailType:o=this.validateEmailField(e),o.errorText=t.incorrectEmail;break;case i.radioType:o.condition=this.validateRadioField(e);break;case i.selectType:o=this.validateSelectField(e),o.errorText=t.incorrectSelect}this.checkFieldValidness(e,o.condition,o.errorText,o.length)},t.prototype.validateTextField=function(e){var t,n,o=this.lang[this.options.lang],a=e.find("input").length?e.find("input"):e.find("textarea"),r=a.val(),l=r.length,d=a.data(i.dataCondition);switch(d){case i.length:var c=a.data("length"),s=a.data("max-length"),h=a.data("min-length");if(c){t=l===parseInt(c,10),n=o.reqFieldLength+c+" "+o.symbols;break}var u=l<=parseInt(s,10),p=l>=parseInt(h,10);s&&h?(t=u&&p,n=o.minMaxFieldLength.first+s+o.minMaxFieldLength.second+h):s?(t=u,n=o.maxFieldLength+s+" "+o.symbols):h&&(t=p,n=o.minFieldLength+h+" "+o.symbols);break;case i.equal:t=r===a.data(i.equal),n=o.notEqual}return{condition:t,errorText:n,length:l}},t.prototype.validateEmailField=function(e){var i=e.find("input").val();return{condition:t.validateEmail(i),length:i.length}},t.prototype.validateRadioField=function(e){return e.find('input[type="radio"]:checked:visible').length>=1},t.prototype.validateSelectField=function(e){var t=e.find("select").val();return{condition:t&&0!=t,length:1}},t.prototype.serializedFormData=function(){return this.$form.serialize()},t.prototype.sendIfValidated=function(t){var i=this,n=i.options;return t=t||n.ajax,i.checkAndRunCallback(n.beforeValidation),this.formIsValid()&&function(){i.checkAndRunCallback(n.onValid),t&&e.ajax(t)}(),i.checkAndRunCallback(n.afterValidation),this},t.prototype.validateAllFields=function(){var t=this;return t.$form.find(t.options.fieldsSelector).each(function(n,o){var a=e(o);a.data("validation")===i.requiredToValidate&&t.validateField(a)}),this},t.prototype.runFormValidation=function(){return this.removeIncorrectState().validateAllFields().sendIfValidated()},t.prototype.bindOnClickValidation=function(){var e=this;return e.$form.find(".validate-form-button").on("click.validation tap.validation",function(t){t.preventDefault(),e.runFormValidation()}),this},t.prototype.unbindOnClick=function(){return this.$form.find(".validate-form-button").unbind("click.validation tap.validation"),this},t.prototype.init=function(){return this.bindOnClickValidation(),this.options.modal&&this.removeIncorrectStateOnModalClose(),this.options.removeErrorOnFocusOut&&this.removeIncorrectStateOnFocusOut(),this};var n=t.expose=function(i){e=i,e.fn.validator=function(i){var n=[];return this.each(function(){var o=e(this);n.push(new t(o,i))}),1===n.length?n[0]:n}};"object"==typeof module&&"object"==typeof module.exports?module.exports=t:"function"==typeof define&&define.amd?define("vintage-popup",function(){return t}):window.Validator=t,n(e)}(jQuery||window.jQuery||window.$);