I need to validate a field in a custom form. The field should be not required, and should validate if the content entered in it are numbers. I added the validation on the form which works. What does not work is the validation of this field.

<input type="number" class="product-quantity" name="qty" data-validate="{ required: false, number: true }"/>

I have tried using number and using validate-number but it does not help. After typing for example abc the validation passes.

Thank you in advance for your help!

2 Answers 2


Would you be able to post the entire form code?

I suspect that you are missing the initialization of the mage/validation library.

Please see here

In order for the validation rules to work. You must initialize the library within the scope of the form

  • Check comment below.
    – mrcl
    Apr 22, 2022 at 11:44

@Ocastro I have something like that. As I said. The other rules work correctly. Only on this field neither number nor validate-number validation works for me.

<script type="text/x-magento-init">
            "#ask-container": {
                "VENDOR_MODULE/js/askAboutAvailability": {
                     "telephone": "<?= $block->getConfigData("telephone"); ?>",
                     "ajaxRequestUrl": "<?= $escaper->escapeUrl($block->getPostUrl()) ?>"

Your Answer

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

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