+ Start a Discussion
Ben Olsen 12Ben Olsen 12 

REGEX phone number validation

I'm getting a syntax error when using this in a validation rule,   any ideas ?      NOT( REGEX(Phone,"[0-9\(\)\s\-\+]"))
Best Answer chosen by Ben Olsen 12
NagaNaga (Salesforce Developers) 
Hi Ben,


Try the below Regex

REGEX =/^[1-9]\d{0,1}(\.\d{2})?$/;
This works fine for 0 to 99.99.

Please let me know if this helps

Best Regards
NagaKiran

All Answers

NagaNaga (Salesforce Developers) 
Hi Ben,


Try the below Regex

REGEX =/^[1-9]\d{0,1}(\.\d{2})?$/;
This works fine for 0 to 99.99.

Please let me know if this helps

Best Regards
NagaKiran
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8
Please try below validation rule

Exmple 1:-
NOT(
   REGEX( Phone, "^\\+([0-9 ]+)$" ) &&
   LEN( SUBSTITUTE(Phone, " ", "") ) < 26 &&
   LEN( SUBSTITUTE(Phone, " ", "") ) > 6
)

Example 2:-
US phone numbers should be in this format: (999) 999-9999
NOT(REGEX(Phone, "\\D*?(\\d\\D*?){10}"))
Please let us know if this will help you

Thanks
Amit Chaudhary
Ben Olsen 12Ben Olsen 12
ok this is good.  Thank you. I also found that   REGEX(Phone, ".{0,}[A-Za-z]+.{0,}") works, but this will only keep out any alphabetical stuff.
Achraf EzzaouiaAchraf Ezzaouia
Hey Ben, 

Is it possible to limit the amount of numbers so the phone number doesn't exceed 10 numbers or 11 if they have to deal 1 before the area code? For example your REGEX(Phone, ".{0,}[A-Za-z]+.{0,}") worked great for accepting these formats xxx-xxx-xxxx, (xxx) xxx-xxxx and xxxxxxxxxx while limiting alphabetical letters which has been my biggest pain point using REGEX so far. It is however allowing numbers this long for example
"5555555555333333" ,Thanks, 
Igor PetrovychIgor Petrovych
If you need a more advanced validation of a number, to make sure you really have a valid number - you can do that in the trigger handler, and use a ported libphonenumber (http://appexchange.salesforce.com/appxListingDetail?listingId=a0N3A00000G12oJUAR) library.