function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Anuj Joshi 42Anuj Joshi 42 

Validation rule for phone number based on user's iso country code

Hi All,

I want to write a validation rule on phone number custom field. I want to check if the phone number cotains the user's iso country code. For example the user creating or updatin the record has its iso country as INDIA. The validation rule must check if the phone number contains +91. I want to write that for many countries.

Thanks,
Anuj
Jithesh VasudevanJithesh Vasudevan
Hi Anuj,

You can use the below code,
IF( Country !='',NOT(REGEX( Phone , "[+][0-9]{2}[' '][0-9]*{10}")),False)
So a phone number like +91 (850) 644-4201 is valid as per the above formula.  And you can give a proper error mesage which could make the user fill the record more correctly.
Anuj Joshi 42Anuj Joshi 42
Hi Jithesh,

I need to check user's country code and then check. If u are an indian user and u enter +92 and then number, it should throw an error.

Thanks,
Anuj
Jithesh VasudevanJithesh Vasudevan
Is there a limit in the number of countries that you use?
Jithesh VasudevanJithesh Vasudevan
Hi Anuj,

You can use the below formula in that case,

IF((MailingCountry='US' || MailingCountry='United States') && LEFT(Phone,2)<>'+1', True, IF(MailingCountry='India'&& LEFT(Phone,3)<>'+91', True, IF((MailingCountry='UK' || MailingCountry='United Kingdom') && LEFT(Phone,3)<>'+44', True, False)))

If you have more mailing countries that you use, just take the core formula out of the above one which is,
IF(MailingCountry='<Country Name>' && (LEFT(Phone,3) <> '+<2digits>' ,True,False) and fill it and paste it in the false part of the above formula and do the same logic oif you have many countries to be used.

Take the left pasrt of phone as per the number of digits contains in a country code. For example, for US, it is '+1'. So take LEFT(Phone,2). And for UK(+44) or India(+91), the same is LEFT(Phone,3).

Do let me know if you have any doubts or queries.
sujan chakravarthysujan chakravarthy
hi All,
Phone Number Should start with +91,+01,+44,+61 following numbers,
how to write error condition for this?
Igor Petrovych 4Igor Petrovych 4
Hey! We have ported the libphonenumber library to Salesforce (Apex). You can use it and custom validation to extract needed information about a phone number and validate it, including country. It's available for free (https://appexchange.salesforce.com/appxListingDetail?listingId=a0N3A00000G12oJUAR). In case you have any issues or need some help don't hesitate to contact me or our team at Noltic (https://noltic.com/).
Shekhar 9Shekhar 9
Hi All, 
I have a requirement where according to the country (picklist: USA, Canada, Australia, UK, India), the phone number must start its respective country code.
IF(( Country__c == 'US')&& LEFT(Phone,2) <> '+1',True,
IF( Country__c == 'India')&& LEFT(Phone,3) <> '+91',True,
IF( Country__c == 'UK')&& LEFT(Phone,3) <> '+44',True,
IF( Country__c == 'Australia')&& LEFT(Phone,3) <> '+61',True,
IF( Country__c == 'Canada')&& LEFT(Phone,2) <> '+1',True, False)

The above throws an error: Field Country__c is a picklist field. Picklist fields are only supported in certain function
Please assist with the correct function for the requirement. 
Daniel Rohberg 10Daniel Rohberg 10
Hello Shekhar 9,

Please try this:

ISPICKVAL(MailingCountryCode , 'US') && LEFT(Phone,1) <> '1' ||
MailingCountry = 'India' && LEFT(Phone,2) <> '91' ||
ISPICKVAL(MailingCountryCode , 'UK') && LEFT(Phone,2) <> '44' ||
MailingCountry = 'Australia' && LEFT(Phone,2) <> '61' ||
MailingCountry = 'Canada' && LEFT(Phone,1) <> '1'
pavan Kumar.vpavan Kumar.v
Hi @sujan,
Did you got the answer.
even I'm looking for regex code where it should accept +61897654324 format.