+ Start a Discussion
Venkata Sravan Kumar BandariVenkata Sravan Kumar Bandari 

can anyone please give me the Regex code for the phone number format (XXX)XXX-XXXX

can anyone please give me the Regex code for the phone number format (XXX)XXX-XXXX 
Best Answer chosen by Venkata Sravan Kumar Bandari
Amit Chaudhary 8Amit Chaudhary 8
Please check below post .

1) http://sfdc2u.blogspot.in/2013/08/phone-number-validation-in-salesforce.html
 
      Pattern p = Pattern.compile('[-() ]');
      String sPhone = p.matcher(account.Phone).replaceAll('');
      // check length without punctuation
      if (sPhone.length() != 10) account.Phone.addError(' Phone number must have 3 digit area code and 7 digit number');
      p = Pattern.compile('\\d');
      sPhone = p.matcher(sPhone).replaceAll('');
      if (sPhone.length() > 0) account.Phone.addError('Phone number must be formatted as (999)999-9999');
Option 2:-
http://forceguru.blogspot.in/2011/05/validation-over-apex.html
 
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;  
   }  
  }  
  else  
  {  
   return false ;  
  }  
 }  
}
http://forcemonkey.blogspot.in/2011/01/coverting-code-to-button-clicks.html

Let us know if this will help you

 

All Answers

BALAJI CHBALAJI CH
Hi Venkata Sravan,

You can use this:-
/^(?:\(\d{3}\)|\d{3}-)\d{3}-\d{4}$/

Please find the link. 
http://stackoverflow.com/questions/13679310/validation-for-xxx-xxx-xxxx-or-xxxxxx-xxxx

Best Regards,
BALAJI
Amit Chaudhary 8Amit Chaudhary 8
Hi Venkata Sravan,

Option 1:-
US Phone Number Has Ten Digits:-

Validates that the Phone number is in (999) 999-9999 format. This works by using the REGEX function to check that the number has ten digits in the (999) 999-9999 format.
 
NOT(REGEX(Phone, "\\D*?(\\d\\D*?){10}"))

Option 2:-
Validation Rule for 10 Digit US phone number with 4 digit extension

https://help.salesforce.com/apex/HTViewSolution?id=000187896&language=en_US

Option 3 :-

10 digits
no spaces
no dashes or parenthesis 
NOT(REGEX( Phone, "(\\d){10}"))
This means that "Only Allow 10 digits". d - stands for DIGITS and {10} - just 10 of them.



Let us know if this will help you

Thanks
Amit Chaudhary

 
Venkata Sravan Kumar BandariVenkata Sravan Kumar Bandari
@Amit

tried with
pattern.matches('(123)123-1234', '\\D*?(\\d\\D*?){10}');

it is giving false
Venkata Sravan Kumar BandariVenkata Sravan Kumar Bandari
@Balaji

How to use it in Apex code. I have requirement in apex
Amit Chaudhary 8Amit Chaudhary 8
Please check below post .

1) http://sfdc2u.blogspot.in/2013/08/phone-number-validation-in-salesforce.html
 
      Pattern p = Pattern.compile('[-() ]');
      String sPhone = p.matcher(account.Phone).replaceAll('');
      // check length without punctuation
      if (sPhone.length() != 10) account.Phone.addError(' Phone number must have 3 digit area code and 7 digit number');
      p = Pattern.compile('\\d');
      sPhone = p.matcher(sPhone).replaceAll('');
      if (sPhone.length() > 0) account.Phone.addError('Phone number must be formatted as (999)999-9999');
Option 2:-
http://forceguru.blogspot.in/2011/05/validation-over-apex.html
 
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;  
   }  
  }  
  else  
  {  
   return false ;  
  }  
 }  
}
http://forcemonkey.blogspot.in/2011/01/coverting-code-to-button-clicks.html

Let us know if this will help you

 
This was selected as the best answer
BALAJI CHBALAJI CH
You can store the RegEx in a string variable and do Pattern Matching.

For Example:
string RegExp = '(\\D?[0-9]{3}\\D?)[0-9]{3}-[0-9]{4}';
string Phone = '(123)456-7890';
If you try with
Pattern.matches(RegExp, Phone);
You will get true.

I would like to know whether it helped you

Best Regards,
BALAJI