From a000a8f0943429a2029aa49ca0589e9ac6577371 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20T=C3=BClek?= Date: Tue, 14 Sep 2021 20:12:08 +0300 Subject: [PATCH] Version 1.0.5 added new methods reload() checkAll() --- dist/jbvalidator.min.js | 2 +- index.html | 99 +++++++++++++++++------------ package.json | 2 +- src/jbvalidator.js | 134 +++++++++++++++++++++++----------------- 4 files changed, 138 insertions(+), 99 deletions(-) diff --git a/dist/jbvalidator.min.js b/dist/jbvalidator.min.js index f44f992..7f3aac2 100644 --- a/dist/jbvalidator.min.js +++ b/dist/jbvalidator.min.js @@ -1 +1 @@ -!function(e){"use strict";e.fn.jbvalidator=function(t){t=e.extend({},{language:"",errorMessage:!0,successClass:!1,html5BrowserDefault:!1,validFeedBackClass:"valid-feedback",invalidFeedBackClass:"invalid-feedback",validClass:"is-valid",invalidClass:"is-invalid"},t);let a={maxValue:"Value must be less than or equal to %s.",minValue:"Value must be greater than or equal to %s.",maxLength:"Please lengthen this text to %s characters or less (you are currently using %s characters).",minLength:"Please lengthen this text to %s characters or more (you are currently using %s characters).",minSelectOption:"Please select at least %s options.",maxSelectOption:"Please select at most %s options.",groupCheckBox:"Please select at least %s options.",equal:"This field does not match with %s field.",fileMinSize:"File size cannot be less than %s bytes.",fileMaxSize:"File size cannot be more than %s bytes.",number:"Please enter a number.",HTML5:{valueMissing:{INPUT:{default:"Please fill out this field.",checkbox:"Please check this box.",radio:"Please select one of these options.",file:"Please select a file."},SELECT:"Please select an item in the list."},typeMismatch:{email:"Please enter an e-mail address.",url:"Please enter a URL."},rangeUnderflow:{date:"Value must be %s or later.",month:"Value must be %s or later.",week:"Value must be %s or later.",time:"Value must be %s or later.",datetimeLocale:"Value must be %s or later.",number:"Value must be greater than or equal to %s.",range:"Value must be greater than or equal to %s."},rangeOverflow:{date:"Value must be %s or earlier.",month:"Value must be %s or earlier.",week:"Value must be %s or earlier.",time:"Value must be %s or earlier.",datetimeLocale:"Value must be %s or earlier.",number:"Value must be less than or equal to %s.",range:"Value must be less than or equal to %s."},stepMismatch:{date:"You can only select every %s. day in the date calendar.",month:"You can only select every %s. month in the date calendar.",week:"You can only select every %s. week in the date calendar.",time:"You can only select every %s. second in the time picker.",datetimeLocale:"You can only select every %s. second in the time picker.",number:"Please enter a valid value. Only %s and a multiple of %s.",range:"Please enter a valid value. Only %s and a multiple of %s."},tooLong:"Please lengthen this text to %s characters or less (you are currently using %s characters).",tooShort:"Please lengthen this text to %s characters or more (you are currently using %s characters).",patternMismatch:"Please match the request format. %s",badInput:{number:"Please enter a number."}}};const i="input, textarea, select";let n=0;t.language&&e.getJSON(t.language,(function(e){a=e})),e(this).on("submit",(function(t){n=0,e(this).find(i).each(((e,a)=>{l(a,t)})),n&&(t.preventDefault(),t.stopPropagation())})),e(this).find(i).each(((t,a)=>{e(a).on("input",(function(t){if(l(this,t),c(a,"data-v-equal")){let t=e(a).attr("data-v-equal");e(t).one("input",(function(){let t=e(this).attr("id");e('[data-v-equal="#'+t+'"]').trigger("input")}))}}))}));let l=function(e,a){e.setCustomValidity(""),!1!==e.checkValidity()?Object.values(o).map((t=>{let i=t.call(t,e,a);i&&e.setCustomValidity(i)})):t.html5BrowserDefault||e.setCustomValidity(u(e)),!1===e.checkValidity()?(r(e,e.validationMessage),n++):s(e)},r=function(a,i){if(e(a).removeClass(t.validClass),e(a).addClass(t.invalidClass),i=e(a).data("vMessage")??i,t.errorMessage){let n=e(a).parent();if(e(n).length){let a=e(n).find("."+t.invalidFeedBackClass);e(a).length?e(a).html(i):e(n).append('
'+i+"
")}}},s=function(a){e(a).removeClass(t.invalidClass),t.successClass&&e(a).addClass(t.validClass)},o={multiSelectMin:function(t){if("SELECT"===e(t).prop("tagName")&&e(t).prop("multiple")){let i=e(t).data("vMinSelect"),n=e(t).find("option:selected").length;if(n0))return a.minSelectOption.sprintf(i)}return""},multiSelectMax:function(t){if("SELECT"===e(t).prop("tagName")&&e(t).prop("multiple")){let i=e(t).data("vMaxSelect"),n=e(t).find("option:selected").length;if(n>i&&(e(t).prop("require")||n>0))return a.maxSelectOption.sprintf(i)}return""},equal:function(t){let i=e(t).data("vEqual");if(i){let n=e(i).attr("title");if(e(i).val()!==e(t).val())return a.equal.sprintf(n||"")}return""},groupCheckBox:function(t,i){if(c(t,"type","checkbox")){let n=e(t).closest("[data-checkbox-group]"),l=e(n).data("vMinSelect"),r=n.find("input[type=checkbox]:checked").length,s=void 0===e(n).data("vRequired")?0:1;if(n&&(void 0!==i.originalEvent&&"input"===i.originalEvent.type&&e(n).find("input[type=checkbox]").each(((t,a)=>{e(a).trigger("input")})),r0)&&!1===e(t).prop("checked")))return a.groupCheckBox.sprintf(l)}return""},customMin:function(t){if(c(t,"data-v-min")){let i=e(t).data("vMin"),n=e(t).val();if(isNaN(n)&&(e(t).prop("require")||n.length>0))return a.number;if(n0))return a.minValue.sprintf(i)}return""},customMax:function(t){if(c(t,"data-v-max")){let i=e(t).data("vMax"),n=e(t).val();if(isNaN(n)&&(e(t).prop("require")||n.length>0))return a.number;if(n>i&&(e(t).prop("require")||n.length>0))return a.maxValue.sprintf(i)}return""},customMinLength:function(t){if(c(t,"data-v-min-length")){let i=e(t).data("vMinLength"),n=e(t).val().length;if(n0))return a.minLength.sprintf(i,n)}return""},customMaxLength:function(t){if(c(t,"data-v-max-length")){let i=e(t).data("vMaxLength"),n=e(t).val().length;if(n>i&&(e(t).prop("require")||n>0))return a.maxLength.sprintf(i,n)}return""},fileMinSize:function(t){if(c(t,"type","file")){let i=e(t).data("vMinSize");for(let e=0;et.files[e].size)return a.fileMinSize.sprintf(i)}return""},fileMaxSize:function(t){if(c(t,"type","file")){let i=e(t).data("vMaxSize");for(let e=0;e1||0==a?"s":"";e=e.replace(/%s {1}(\w+)\(s\){1}/,"%s $1"+i)}e=e.replace("%s",arguments[t])}return e},{validator:o,errorTrigger:function(e,t){"object"==typeof e&&(e=e[0]),e.setCustomValidity(t),r(e,e.validationMessage)}}}}(jQuery); \ No newline at end of file +!function(e){"use strict";e.fn.jbvalidator=function(t){t=e.extend({},{language:"",errorMessage:!0,successClass:!1,html5BrowserDefault:!1,validFeedBackClass:"valid-feedback",invalidFeedBackClass:"invalid-feedback",validClass:"is-valid",invalidClass:"is-invalid"},t);let a=this,i={maxValue:"Value must be less than or equal to %s.",minValue:"Value must be greater than or equal to %s.",maxLength:"Please lengthen this text to %s characters or less (you are currently using %s characters).",minLength:"Please lengthen this text to %s characters or more (you are currently using %s characters).",minSelectOption:"Please select at least %s options.",maxSelectOption:"Please select at most %s options.",groupCheckBox:"Please select at least %s options.",equal:"This field does not match with %s field.",fileMinSize:"File size cannot be less than %s bytes.",fileMaxSize:"File size cannot be more than %s bytes.",number:"Please enter a number.",HTML5:{valueMissing:{INPUT:{default:"Please fill out this field.",checkbox:"Please check this box.",radio:"Please select one of these options.",file:"Please select a file."},SELECT:"Please select an item in the list."},typeMismatch:{email:"Please enter an e-mail address.",url:"Please enter a URL."},rangeUnderflow:{date:"Value must be %s or later.",month:"Value must be %s or later.",week:"Value must be %s or later.",time:"Value must be %s or later.",datetimeLocale:"Value must be %s or later.",number:"Value must be greater than or equal to %s.",range:"Value must be greater than or equal to %s."},rangeOverflow:{date:"Value must be %s or earlier.",month:"Value must be %s or earlier.",week:"Value must be %s or earlier.",time:"Value must be %s or earlier.",datetimeLocale:"Value must be %s or earlier.",number:"Value must be less than or equal to %s.",range:"Value must be less than or equal to %s."},stepMismatch:{date:"You can only select every %s. day in the date calendar.",month:"You can only select every %s. month in the date calendar.",week:"You can only select every %s. week in the date calendar.",time:"You can only select every %s. second in the time picker.",datetimeLocale:"You can only select every %s. second in the time picker.",number:"Please enter a valid value. Only %s and a multiple of %s.",range:"Please enter a valid value. Only %s and a multiple of %s."},tooLong:"Please lengthen this text to %s characters or less (you are currently using %s characters).",tooShort:"Please lengthen this text to %s characters or more (you are currently using %s characters).",patternMismatch:"Please match the request format. %s",badInput:{number:"Please enter a number."}}};const n="input, textarea, select";let l=0;t.language&&e.getJSON(t.language,(function(e){i=e})),e(a).on("submit",(function(e){l=0,r(e),l&&(e.preventDefault(),e.stopPropagation())}));let r=function(t){return l=0,e(a).find(n).each(((e,a)=>{o(a,t)})),l},s=function(){e(a).find(n).each(((t,a)=>{e(a).off("input"),e(a).on("input",(function(t){if(o(this,t),p(a,"data-v-equal")){let t=e(a).attr("data-v-equal");e(t).one("input",(function(){let t=e(this).attr("id");e('[data-v-equal="#'+t+'"]').trigger("input")}))}}))}))},o=function(e,a){e.setCustomValidity(""),!1!==e.checkValidity()?Object.values(f).map((t=>{let i=t.call(t,e,a);i&&e.setCustomValidity(i)})):t.html5BrowserDefault||e.setCustomValidity(d(e)),!1===e.checkValidity()?(u(e,e.validationMessage),l++):c(e)},u=function(a,i){if(e(a).removeClass(t.validClass),e(a).addClass(t.invalidClass),i=e(a).data("vMessage")??i,t.errorMessage){let n=e(a).parent();if(e(n).length){let a=e(n).find("."+t.invalidFeedBackClass);e(a).length?e(a).html(i):e(n).append('
'+i+"
")}}},c=function(a){e(a).removeClass(t.invalidClass),t.successClass&&e(a).addClass(t.validClass)},f={multiSelectMin:function(t){if("SELECT"===e(t).prop("tagName")&&e(t).prop("multiple")){let a=e(t).data("vMinSelect"),n=e(t).find("option:selected").length;if(n0))return i.minSelectOption.sprintf(a)}return""},multiSelectMax:function(t){if("SELECT"===e(t).prop("tagName")&&e(t).prop("multiple")){let a=e(t).data("vMaxSelect"),n=e(t).find("option:selected").length;if(n>a&&(e(t).prop("require")||n>0))return i.maxSelectOption.sprintf(a)}return""},equal:function(t){let a=e(t).data("vEqual");if(a){let n=e(a).attr("title");if(e(a).val()!==e(t).val())return i.equal.sprintf(n||"")}return""},groupCheckBox:function(t,a){if(p(t,"type","checkbox")){let n=e(t).closest("[data-checkbox-group]"),l=e(n).data("vMinSelect"),r=n.find("input[type=checkbox]:checked").length,s=void 0===e(n).data("vRequired")?0:1;if(n&&(void 0!==a.originalEvent&&"input"===a.originalEvent.type&&e(n).find("input[type=checkbox]").each(((t,a)=>{e(a).trigger("input")})),r0)&&!1===e(t).prop("checked")))return i.groupCheckBox.sprintf(l)}return""},customMin:function(t){if(p(t,"data-v-min")){let a=e(t).data("vMin"),n=e(t).val();if(isNaN(n)&&(e(t).prop("require")||n.length>0))return i.number;if(n0))return i.minValue.sprintf(a)}return""},customMax:function(t){if(p(t,"data-v-max")){let a=e(t).data("vMax"),n=e(t).val();if(isNaN(n)&&(e(t).prop("require")||n.length>0))return i.number;if(n>a&&(e(t).prop("require")||n.length>0))return i.maxValue.sprintf(a)}return""},customMinLength:function(t){if(p(t,"data-v-min-length")){let a=e(t).data("vMinLength"),n=e(t).val().length;if(n0))return i.minLength.sprintf(a,n)}return""},customMaxLength:function(t){if(p(t,"data-v-max-length")){let a=e(t).data("vMaxLength"),n=e(t).val().length;if(n>a&&(e(t).prop("require")||n>0))return i.maxLength.sprintf(a,n)}return""},fileMinSize:function(t){if(p(t,"type","file")){let a=e(t).data("vMinSize");for(let e=0;et.files[e].size)return i.fileMinSize.sprintf(a)}return""},fileMaxSize:function(t){if(p(t,"type","file")){let a=e(t).data("vMaxSize");for(let e=0;e1||0==a?"s":"";e=e.replace(/%s {1}(\w+)\(s\){1}/,"%s $1"+i)}e=e.replace("%s",arguments[t])}return e},s(),{validator:f,errorTrigger:function(e,t){"object"==typeof e&&(e=e[0]),e.setCustomValidity(t),u(e,e.validationMessage)},reload:function(){s()},checkAll:r}}}(jQuery); \ No newline at end of file diff --git a/index.html b/index.html index c55e3ea..d5a3259 100644 --- a/index.html +++ b/index.html @@ -38,6 +38,15 @@
GITHUB
  • data-v-message: alternative error mesage
  • +
    +

    Methods

    +
      +
    • validator: add new custom validation
    • +
    • checkAll(): show errors without submitting the form, return error count
    • +
    • errorTrigger(): show the error messages returned from the server.
    • +
    • reload(): reload instance after dynamic element is added
    • +
    +

    Usage

    @@ -58,11 +67,62 @@

    Usage

    return 'Your password is too weak.'; } } + + validator.validator.example = function(el, event){ + if($(el).is('[name=username]') && $(el).val().length < 3){ + return 'Your username is too short.'; + } + } + + //check form without submit + validator.checkAll(); //return error count + + //reload instance after dynamic element is added + validator.reload(); }) </script>
    +
    +

    Serverside validation

    +

    You can show the error messages returned from the server. The ".errorTrigger" method can be used for + this.

    +

    .errorTrigger(element, message)

    +
    + +
    +        <script src="dist/jbvalidator.min.js"></script>
    +        <script>
    +            $(function (){
    +
    +               let validatorServerSide = $('form.validatorServerSide').jbvalidator({
    +                    errorMessage: true,
    +                    successClass: false,
    +                });
    +
    +                //serverside
    +                $(document).on('submit', '.validatorServerSide', function(){
    +
    +                    $.ajax({
    +                        method:"get",
    +                        url:"http://jsvalidation.test/test.json",
    +                        data: $(this).serialize(),
    +                        success: function (data){
    +                            if(data.status === 'error') {
    +                                validatorServerSide.errorTrigger($('[name=username]'), data.message);
    +                            }
    +                        }
    +                    })
    +
    +                    return false;
    +                });
    +            })
    +        </script>
    +        
    +
    +
    +

    Options

    @@ -489,45 +549,7 @@ 

    Floating labels

    -
    -

    Serverside validation

    -

    You can show the error messages returned from the server. The ".errorTrigger" method can be used for - this.

    -

    .errorTrigger(element, message)

    -
    - -
    -        <script src="dist/jbvalidator.min.js"></script>
    -        <script>
    -            $(function (){
     
    -               let validatorServerSide = $('form.validatorServerSide').jbvalidator({
    -                    errorMessage: true,
    -                    successClass: false,
    -                });
    -
    -                //serverside
    -                $(document).on('submit', '.validatorServerSide', function(){
    -
    -                    $.ajax({
    -                        method:"get",
    -                        url:"http://jsvalidation.test/test.json",
    -                        data: $(this).serialize(),
    -                        success: function (data){
    -                            if(data.status === 'error') {
    -                                validatorServerSide.errorTrigger($('[name=username]'), data.message);
    -                            }
    -                        }
    -                    })
    -
    -                    return false;
    -                });
    -            })
    -        </script>
    -        
    -
    -
    -
    @@ -552,7 +574,6 @@

    Serverside validation

    - diff --git a/package.json b/package.json index f6ae08b..e25033a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@emretulek/jbvalidator", - "version": "1.0.4", + "version": "1.0.5", "repository": { "type": "git", "url": "git+https://github.com/emretulek/jbvalidator.git" diff --git a/src/jbvalidator.js b/src/jbvalidator.js index d84e1a2..e49a273 100644 --- a/src/jbvalidator.js +++ b/src/jbvalidator.js @@ -103,10 +103,7 @@ STATUS = 0; - $(this).find(selector).each((i, el) => { - - validationRun(el, event); - }); + checkAll(event); if (STATUS) { event.preventDefault(); @@ -114,25 +111,38 @@ } }); + + let checkAll = function (event) { + + STATUS = 0; + $(FORM).find(selector).each((i, el) => { + validationRun(el, event); + }); + + return STATUS; + } + /** * run validate when on input */ - $(FORM).find(selector).each((i, el) => { + let run = function () { + $(FORM).find(selector).each((i, el) => { - $(el).on('input', function (event) { + $(el).off('input'); + $(el).on('input', function (event) { - validationRun(this, event); + validationRun(this, event); - if (hasAttr(el, 'data-v-equal')) { - let equal = $(el).attr('data-v-equal'); - $(equal).one('input', function (){ - let id = $(this).attr('id'); - $('[data-v-equal="#' + id + '"]').trigger('input'); - }); - } + if (hasAttr(el, 'data-v-equal')) { + let equal = $(el).attr('data-v-equal'); + $(equal).one('input', function () { + let id = $(this).attr('id'); + $('[data-v-equal="#' + id + '"]').trigger('input'); + }); + } + }) }) - }) - + } let validationRun = function (el, event) { @@ -144,12 +154,12 @@ let error = value.call(value, el, event); - if(error) { + if (error) { el.setCustomValidity(error); } }); } else { - if(!options.html5BrowserDefault) { + if (!options.html5BrowserDefault) { el.setCustomValidity(HTML5Default(el)) } } @@ -178,11 +188,11 @@ let group = $(el).parent(); if ($(group).length) { - let invalidFeedBack = $(group).find('.'+ options.invalidFeedBackClass); + let invalidFeedBack = $(group).find('.' + options.invalidFeedBackClass); if ($(invalidFeedBack).length) { $(invalidFeedBack).html(message); } else { - $(group).append('
    ' + message + '
    '); + $(group).append('
    ' + message + '
    '); } } } @@ -199,7 +209,7 @@ let validator = { - multiSelectMin : function (el) { + multiSelectMin: function (el) { if ($(el).prop("tagName") === "SELECT" && $(el).prop('multiple')) { @@ -213,7 +223,7 @@ return ''; }, - multiSelectMax : function (el) { + multiSelectMax: function (el) { if ($(el).prop("tagName") === "SELECT" && $(el).prop('multiple')) { @@ -227,7 +237,7 @@ return ''; }, - equal : function (el) { + equal: function (el) { let equal = $(el).data('vEqual'); @@ -241,7 +251,7 @@ return ''; }, - groupCheckBox : function (el, event) { + groupCheckBox: function (el, event) { if (hasAttr(el, 'type', 'checkbox')) { @@ -252,7 +262,7 @@ if (checkGroup) { - if(typeof event.originalEvent !== "undefined" && event.originalEvent.type === 'input') { + if (typeof event.originalEvent !== "undefined" && event.originalEvent.type === 'input') { $(checkGroup).find('input[type=checkbox]').each((i, item) => { $(item).trigger('input'); }) @@ -268,7 +278,7 @@ return ''; }, - customMin : function (el) { + customMin: function (el) { if (hasAttr(el, 'data-v-min')) { @@ -288,7 +298,7 @@ return ''; }, - customMax : function (el) { + customMax: function (el) { if (hasAttr(el, 'data-v-max')) { @@ -308,7 +318,7 @@ return ''; }, - customMinLength : function (el) { + customMinLength: function (el) { if (hasAttr(el, 'data-v-min-length')) { @@ -322,7 +332,7 @@ return ''; }, - customMaxLength : function (el) { + customMaxLength: function (el) { if (hasAttr(el, 'data-v-max-length')) { @@ -336,7 +346,7 @@ return ''; }, - fileMinSize : function (el) { + fileMinSize: function (el) { if (hasAttr(el, 'type', 'file')) { @@ -352,7 +362,7 @@ return ''; }, - fileMaxSize : function (el) { + fileMaxSize: function (el) { if (hasAttr(el, 'type', 'file')) { @@ -375,90 +385,90 @@ * @returns {null|jQuery|HTMLElement|undefined|string|*} * @constructor */ - let HTML5Default = function (el){ + let HTML5Default = function (el) { - if(el.validity.valueMissing){ - if(el.tagName === 'INPUT'){ + if (el.validity.valueMissing) { + if (el.tagName === 'INPUT') { - if(typeof errorMessages.HTML5.valueMissing.INPUT[el.type] === 'undefined') { + if (typeof errorMessages.HTML5.valueMissing.INPUT[el.type] === 'undefined') { return errorMessages.HTML5.valueMissing.INPUT.default; - }else { + } else { return errorMessages.HTML5.valueMissing.INPUT[el.type]; } - }else { + } else { - if(typeof errorMessages.HTML5.valueMissing[el.tagName] !== 'undefined') { + if (typeof errorMessages.HTML5.valueMissing[el.tagName] !== 'undefined') { return errorMessages.HTML5.valueMissing[el.tagName]; } } - }else if(el.validity.typeMismatch){ + } else if (el.validity.typeMismatch) { - if(typeof errorMessages.HTML5.typeMismatch[el.type] !== 'undefined') { + if (typeof errorMessages.HTML5.typeMismatch[el.type] !== 'undefined') { return errorMessages.HTML5.typeMismatch[el.type]; } - }else if(el.validity.rangeOverflow){ + } else if (el.validity.rangeOverflow) { - if(typeof errorMessages.HTML5.rangeOverflow[el.type] !== 'undefined') { + if (typeof errorMessages.HTML5.rangeOverflow[el.type] !== 'undefined') { let max = el.getAttribute('max') ?? null; - if(el.type === 'date' || el.type === 'month'){ + if (el.type === 'date' || el.type === 'month') { let date = new Date(max); max = date.toLocaleDateString(); } - if(el.type === 'week'){ + if (el.type === 'week') { max = "Week " + max.substr(6); } return errorMessages.HTML5.rangeOverflow[el.type].sprintf(max); } - }else if(el.validity.rangeUnderflow){ + } else if (el.validity.rangeUnderflow) { - if(typeof errorMessages.HTML5.rangeUnderflow[el.type] !== 'undefined') { + if (typeof errorMessages.HTML5.rangeUnderflow[el.type] !== 'undefined') { let min = el.getAttribute('min') ?? null; - if(el.type === 'date' || el.type === 'month'){ + if (el.type === 'date' || el.type === 'month') { let date = new Date(min); min = date.toLocaleDateString(); } - if(el.type === 'week'){ + if (el.type === 'week') { min = "Week " + min.substr(6); } return errorMessages.HTML5.rangeUnderflow[el.type].sprintf(min); } - }else if(el.validity.stepMismatch){ + } else if (el.validity.stepMismatch) { - if(typeof errorMessages.HTML5.stepMismatch[el.type] !== 'undefined') { + if (typeof errorMessages.HTML5.stepMismatch[el.type] !== 'undefined') { let step = el.getAttribute('step') ?? null; - if(el.type === 'date' || el.type === 'month'){ + if (el.type === 'date' || el.type === 'month') { let date = new Date(step); step = date.toLocaleDateString(); } - if(el.type === 'week'){ + if (el.type === 'week') { step = "Week " + step.substr(6); } return errorMessages.HTML5.stepMismatch[el.type].sprintf(step, step); } - }else if(el.validity.tooLong){ + } else if (el.validity.tooLong) { let minLength = el.getAttribute('maxlength') ?? null; let value = $(el).val(); return errorMessages.HTML5.tooLong.sprintf(minLength, value.length); - }else if(el.validity.tooShort){ + } else if (el.validity.tooShort) { let maxLength = el.getAttribute('minlength') ?? null; let value = $(el).val(); return errorMessages.HTML5.tooShort.sprintf(maxLength, value.length); - }else if(el.validity.patternMismatch){ + } else if (el.validity.patternMismatch) { if (hasAttr(el, 'pattern') && hasAttr(el, 'title')) { return $(el).attr('title'); @@ -466,9 +476,9 @@ let pattern = el.getAttribute('pattern') ?? null; return errorMessages.HTML5.patternMismatch.sprintf(pattern); - }else if(el.validity.badInput){ + } else if (el.validity.badInput) { - if(typeof errorMessages.HTML5.badInput[el.type] !== 'undefined') { + if (typeof errorMessages.HTML5.badInput[el.type] !== 'undefined') { return errorMessages.HTML5.badInput[el.type]; } @@ -493,6 +503,10 @@ showErrorMessage(el, el.validationMessage); } + let reload = function () { + run(); + } + /** * attr equal with value or has attr * @param el @@ -534,9 +548,13 @@ return output; } + run(); + return { validator, - errorTrigger + errorTrigger, + reload, + checkAll } } })(jQuery);