Magento – Magento 2 minimum length validation is not working for text field

form-validationjavascriptmagento2

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">

Best Answer

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);
        }
    ],