Wednesday, March 4, 2020

How to validate mobile with country code in Dynamic CRM

One of my clients requested to validate mobile phone number along with country code.

In India mobile number format should be like  +91 1234567890 where +91 is a country code and actual contact number is of 10 digits.

In general, for other country as well we should have minimum of 10-digit validations (citation needed)

Here are some testing screen shots for you to understand and visualize.
Images are self-explanatory.

For formatting a number in US format refer here









JavaScript code to validate for such a requirement.

updatePhoneNumberFormat: function (executionContext, phoneFieldName, phoneLabel)
{
var phoneCleaned;
var phoneNumber = base.attr.getValue(executionContext, phoneFieldName);
if (phoneNumber != null) {
    if (phoneNumber.substr(0, 1) != '+') {
        base.attr.getControl(executionContext, phoneFieldName).clearNotification();
        base.attr.getControl(executionContext, phoneFieldName).setNotification(phoneLabel + ' should be formatted with country code +XX');
        return;
    }
    else {
        if (phoneNumber.substr(0, 3) == '+91') {
            phoneCleaned = phoneNumber.replace(/[^0-9]/g, "");
            if (phoneCleaned.length != 12) {
                base.attr.getControl(executionContext, phoneFieldName).clearNotification();
                base.attr.getControl(executionContext, phoneFieldName).setNotification(phoneLabel + ' for India country code should be of 10 digit excluding +91');
                return;
            }
        }
        else {
            phoneCleaned = phoneNumber.replace(/[^0-9]/g, "");
            if (phoneCleaned.length < 11) {
                base.attr.getControl(executionContext, phoneFieldName).clearNotification();
                base.attr.getControl(executionContext, phoneFieldName).setNotification(phoneLabel + ' seems to be too short');
                return;
            }
        }
    }
    base.attr.getControl(executionContext, phoneFieldName).clearNotification();
}
}

I hope you find this as useful.

You can reach out to me for such validation for your dynamic CRM Project.


Thanks.
Vipin Jaiswal
vipinjaiswal12@gmail.com 







No comments: