2

Hello i want to validate phone number in my checkout page but i want phone number validation by their country ,if customer select the USA as country then phone number should accept only usa number if customer select China as country then phone number accept only china number how can i do that?

1

Validation rules are written in validation.js (lib/web/mage/validation.js), so you need override it. requirejs-config.js

var config = {
    map: {
        '*': {
            "Magento_Ui/js/lib/validation/rules": "Vendor_Module/js/lib/validation/rules"
        }
    }
};

rules.js: add rule like this but for countries you want to check.

'phoneUK': [
            function (value) {
                return utils.isEmpty(value) || value.length > 9 &&
                    value.match(/^(\(?(0|\+44)[1-9]{1}\d{1,4}?\)?\s?\d{3,4}\s?\d{3,4})$/);
            },
            $.mage.__('Please specify a valid phone number')
        ],

Referencec Url: https://www.bitbull.it/en/blog/custom-js-validation-magento2/

You can add your custom rule.

| improve this answer | |
  • Your answer is good but i want when the use select the country then selected country validation should fire. Your answer is for specific country validation @RonakChauhan – Magento Oct 18 '19 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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