Magento Adding custom validations-(Phone Number)

Magento doesn’t have any validation for a ten digit mobile number validation.For getting this there are some many ways.This method is taking about overriding the inbuilt validation rules.

For this we have to Create a custom module for the theme , for reference: https://devdocs.magento.com/guides/v2.3/frontend-dev-guide/themes/theme-create.html

after that inside the create path Magento Ui >Magento_Ui/web/js/lib/validation/rules.js

Here we can copy down all validations from vendor magento_ui validation and add custom validations

for adding mobile number validation :

        'mobile-number-validation': [
            function (value) {
                 var p = /^(?!0*$)[0-9-]+/;
                return (value.length == 10 && (p.test(value) == true) );
            },
            $.mage.__('Please enter 10 digit number')
        ]

After this, call mobile-number-validation inside the fields class.

AttributeMergerPlugin can be used to add custom validations to the fieldset in the checkout page.

Leave a comment

Your email address will not be published. Required fields are marked *