From b175062400a3920f546592cdbe270728d5c35700 Mon Sep 17 00:00:00 2001 From: Tony Air Date: Sun, 11 Oct 2020 23:09:12 +0700 Subject: [PATCH] IMPR: JS validate Min/max length --- src/js/_components/_ui.form.validate.field.js | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/js/_components/_ui.form.validate.field.js b/src/js/_components/_ui.form.validate.field.js index 53349b3..9236dc5 100755 --- a/src/js/_components/_ui.form.validate.field.js +++ b/src/js/_components/_ui.form.validate.field.js @@ -70,11 +70,39 @@ const FormValidateField = (($) => { // validate URL if ($el.hasClass('url') && val.length && !this.valideURL(val)) { valid = false; + msg = 'URL must start with http:// or https://. For example: https://your-domain.com/'; console.warn(`${NAME}: Wrong URL #${$el.attr('id')}`); } + let unmaskedVal = val; + if(typeof $el.inputmask === 'function'){ + unmaskedVal = $el.inputmask('unmaskedvalue'); + } + + // maxlength + const maxLength = $el.attr('maxlength'); + if(maxLength && maxLength.length) { + if(unmaskedVal.length > maxLength){ + valid = false; + + msg = `The value is limited to ${maxLength} chars`; + console.warn(`${NAME}: Too long value #${$el.attr('id')}`); + } + } + + // minlength + const minLength = $el.attr('minlength'); + if(minLength && minLength.length) { + if(unmaskedVal.length < minLength){ + valid = false; + + msg = `The value should contain more than ${minLength} chars`; + console.warn(`${NAME}: Too short value #${$el.attr('id')}`); + } + } + this.removeError(); // extra checks