+ Start a Discussion

Validate phone number

Hi, I want my phone number field number to display the number in the format of +XX (XXX) XXX-XXXX. Can someone help me how to achieve this.

Thanks in Advance...
VinayVinay (Salesforce Developers) 

Try below it validates that the number is in (999) 999-9999 format.
NOT(REGEX(Phone, "\\D*?(\\d\\D*?){10}"))


Dushyant SonwarDushyant Sonwar
If you are using it in vf page , then salesforce has already given standard function

You can use formatPhone function of salesforce in inputfield or inputtext
<apex:inputfield value="{!yourbindedfield}" onblur="formatPhone(this);" />

How this function works, is written in below article.
https://help.salesforce.com/articleView?id=000330422&type=1&mode=1 (https://help.salesforce.com/articleView?id=000330422&type=1&mode=1)

Hope this helps!
Akshay Dhiman 63Akshay Dhiman 63
Hi Sai Vineeth,
Here is the Example to validate the phone number in this +XX (XXX) XXX-XXXX format. If you are using the phone field of any salesforce object then you can use this.

public boolean validatePhone(account acc){
    if (acc.Phone != null){
        String phoneNumber = acc.Phone ;  
        Pattern phonePattern = Pattern.compile('\\D*?(\\d\\D*?){10}');  
        Pattern numericPattern = Pattern.compile('[0-9]{10}');  
        Matcher phoneMatcher = phonePattern.matcher(phoneNumber);  
        if(phoneNumber.length() == 10) {
            Matcher numericMatcher = numericPattern.matcher(phoneNumber);  
            if(numericMatcher.matches()) {
                return true;  
            }else {  
                return false;  
            return false ;  

Hope this explanation will resolve your query. Mark it as the best answer if you find it helpful.