2

I want to add range validation for mobile , i have added "validation-digits-range" rule in ui component xml. I have declared range as 9-13 and I enter correct 10 digits still asking "The value is not within the specified range."

here is the ui component code

<field name="mobile_no">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="source" xsi:type="string">customer</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">true</item>
                        <item name="validate-digits" xsi:type="boolean">true</item>
                        <item name="validate-digits-range" xsi:type="string">9-13</item>
                    </item>
                </item>
            </argument>
        </field>

here is the screen shot of customer form enter image description here

1 Answer 1

0

That's because the validation rule you are using looks like this:

    'validate-digits-range': [
        function (value, param) {
            var numValue, dataAttrRange, result, range, m;

            if (utils.isEmptyNoTrim(value)) {
                return true;
            }

            numValue = utils.parseNumber(value);

            if (isNaN(numValue)) {
                return false;
            }

            dataAttrRange = /^(-?\d+)?-(-?\d+)?$/;
            result = true;
            range = param;

            if (range) {
                m = dataAttrRange.exec(range);

                if (m) {
                    result = result && utils.isBetween(numValue, m[1], m[2]);
                }
            }

            return result;
        },
        $.mage.__('The value is not within the specified range.')
    ],

as you can see, the utils.isBetween code just checks that a whole value of the input field greater than 9 and less than 13. In your case you should use the custom validation rule or min_text_length and max_text_length rules.

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.