0

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

1

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

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

@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()) ?>"
            }
       }
  }
</script>

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.