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
sheeba Ssheeba S 

Phone code should displayed according to country

Hi all,

I want to write a validation rule where picklist value country is US the phone number should display with country code +1 and Australia +61 and India  is +91 
 I have tried this 

ISPICKVAL( Country__c , "UK")  

AND(NOT(ISBLANK(Phone_c)),
NOT(REGEX(Phone,"[+44]{3}[1-9]{1}[0-9]{9}"))

 
ISPICKVAL( Country__c , "USA")  
 AND(NOT(ISBLANK(Phone_c)),
NOT(REGEX(Phone,"[+1]{2}[1-9]{1}[0-9]{9}"))

ISPICKVAL( Country__c , "India")  
 
AND( NOT(ISBLANK(Phone_c)),
NOT(REGEX(Phone,"[+91]{3}[1-9]{1}[0-9]{9}"))

ISPICKVAL( Country__c , "Australia")  
 AND(NOT(ISBLANK(Phone_c)),
NOT(REGEX(Phone,"[+61]{3}[1-9]{1}[0-9]{9}"))

)

Thank you in advance 
sheeba
Best Answer chosen by sheeba S
Jithesh VasudevanJithesh Vasudevan
And this one coulf solve that issue,

IF(ISPICKVAL( Country__c , 'USA') && NOT(ISBLANK(Phone)) &&NOT(REGEX(Phone,"[+1]{2}[1-9]{1}[0-9]{9}")), 
TRUE, 
IF(ISPICKVAL(Country__c,'India') && NOT(ISBLANK(Phone)) && NOT(REGEX(Phone,"[+91]{3}[1-9]{1}[0-9]{9}")), 
TRUE, 
IF(ISPICKVAL(Country__c,'UK') && NOT(ISBLANK(Phone)) && NOT(REGEX(Phone,"[+44]{3}[1-9]{1}[0-9]{9}")), 
TRUE, 
IF(ISPICKVAL(Country__c,'Australia') && NOT(ISBLANK(Phone)) && NOT(REGEX(Phone,"[+61]{3}[1-9]{1}[0-9]{9}")), 
TRUE, 
IF(ISPICKVAL(Country__c,' ') && NOT(ISBLANK(Phone)) && NOT(REGEX(Phone,"[0-9]{10}")), 
TRUE, 
FALSE)))))


You can change it as per your needs.

All Answers

Jithesh VasudevanJithesh Vasudevan
You can use this,

IF(ISPICKVAL( Country__c , 'USA') && NOT(REGEX(Phone,"[+1]{2}[1-9]{1}[0-9]{9}")), 
TRUE, 
IF(ISPICKVAL(Country__c,'India') && NOT(REGEX(Phone,"[+91]{3}[1-9]{1}[0-9]{9}")), 
TRUE, 
IF(ISPICKVAL(Country__c,'UK') && NOT(REGEX(Phone,"[+44]{3}[1-9]{1}[0-9]{9}")), 
TRUE, 
IF(ISPICKVAL(Country__c,'Australia') && NOT(REGEX(Phone,"[+61]{3}[1-9]{1}[0-9]{9}")), 
TRUE, 
FALSE))))


However, this has no validation on the Phone field if User selects None as the Country. Meaning, if you have not selected any country, user can type in a 11 digit number or a 5 digits one. Record will still get saved.


 
Jithesh VasudevanJithesh Vasudevan
And this one coulf solve that issue,

IF(ISPICKVAL( Country__c , 'USA') && NOT(ISBLANK(Phone)) &&NOT(REGEX(Phone,"[+1]{2}[1-9]{1}[0-9]{9}")), 
TRUE, 
IF(ISPICKVAL(Country__c,'India') && NOT(ISBLANK(Phone)) && NOT(REGEX(Phone,"[+91]{3}[1-9]{1}[0-9]{9}")), 
TRUE, 
IF(ISPICKVAL(Country__c,'UK') && NOT(ISBLANK(Phone)) && NOT(REGEX(Phone,"[+44]{3}[1-9]{1}[0-9]{9}")), 
TRUE, 
IF(ISPICKVAL(Country__c,'Australia') && NOT(ISBLANK(Phone)) && NOT(REGEX(Phone,"[+61]{3}[1-9]{1}[0-9]{9}")), 
TRUE, 
IF(ISPICKVAL(Country__c,' ') && NOT(ISBLANK(Phone)) && NOT(REGEX(Phone,"[0-9]{10}")), 
TRUE, 
FALSE)))))


You can change it as per your needs.
This was selected as the best answer
RAJAT MESHRAMRAJAT MESHRAM
but this code is giving error 
RAJAT MESHRAMRAJAT MESHRAM
i'm having one country field and one custom primary contact field but when i wrote this validation rule after changing  field names its giving error