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?

2
  • 1
    You should use this github.com/jackocnr/intl-tel-input. We have used in one of our magento instance and its work well with phone number validation as per selected country. Oct 21, 2019 at 5:05
  • @JaiminSutariya Can you help me ? I don't understand how to load UtilsScript, iti.isValidNumber() return null
    – Kevin ami
    Nov 10, 2022 at 10:25

1 Answer 1

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.

1
  • 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 Oct 18, 2019 at 15:58

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.