2

Magento 2 minimum length validation is not working for text field. My code lines:

<input type="text" tabindex="3" 
    class="input-text required-entry validate-length maximum-length-8 minimum-length-4 validate-digits" 
    id="daytime"  name="daytime">
4
  • <input type="text" tabindex="3" class="input-text required-entry validate-length maximum-length-8 minimum-length-4 validate-digits" id="daytime" name="daytime">
    – dev
    Feb 2, 2017 at 14:53
  • do other validators work for this element?
    – Max
    Feb 2, 2017 at 15:16
  • yes maximum-length-8 its working
    – dev
    Feb 3, 2017 at 9:32
  • 2
    looks like this bug already fixed, but commit presented only in develop branch. github.com/magento/magento2/pull/4051/commits/…
    – Max
    Feb 3, 2017 at 9:42

2 Answers 2

2

It's a default bug of Magento 2.x.

if you want a temporary solution you can change lib/web/mage/validation.js of validate-length function.

"validate-length": [
        function (v, elm) {
            var reMax = new RegExp(/^maximum-length-[0-9]+$/),
                reMin = new RegExp(/^minimum-length-[0-9]+$/),
                validator = this,
                result = true,
                length = 0;
            $.each(elm.className.split(' '), function (index, name) {
                if (name.match(reMax) && result) {
                    length = name.split('-')[2];
     /* Need to change message for maximum length validation*/
                    validator.attrLength = 'Maximum length of this field must be equal or less than '+ length +' symbols.';
                    result = (v.length <= length);
                }
                if (name.match(reMin) && result && !$.mage.isEmpty(v)) {
                    length = name.split('-')[2];
     /* Need to change message for minimum length validation*/
                    validator.attrLength = 'Minimum length of this field must be equal or greater than '+ length +' symbols.';
                    result = v.length >= length;
                }
            });
            return result;
        }, function () {
   /* Need to change for displaying proper message */
            return $.mage.__("%1")
                .replace('%1', this.attrLength);
        }
    ],
1
<input type="text" 
       tabindex="3" 
       class="input-text required-entry validate-length maximum-length-8 minimum-length-4 validate-digits"        
       id="daytime"  
       name="daytime"
       minlength="4"
       maxlength="8">

you can use minlength html attribute to validate, another one way to add in class only minimum-length-4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.