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
Michael Sabel 9Michael Sabel 9 

How to get Regex phone number validation rule to work?

Hi, I'm using this validation rule to ensure users type in phone numbers in this format (xxx) xxx-xxxx:
NOT(REGEX(Phone, "\\D*?(\\d\\D*?){10}"))
 
I found this specific vr in Salesforce Help.
This is for Contacts.
Problem I'm having: The validation rule is firing even when the number is inputted in the right format. I tried it in production, developer account and sandbox and it is still doing the same thing.
Thanks.
Best Answer chosen by Michael Sabel 9
SarvaniSarvani
Michael,

Please try below for the fromat (999) 999-9999:
AND( 
NOT(ISBLANK(Phone)), 
NOT(REGEX(Phone, "\\([0-9]{3}\\) [0-9]{3}-[0-9]{4}")) 
)

Thanks

All Answers

SarvaniSarvani
Hi Michael,

Please try formula below:
AND( 
NOT(ISBLANK(Phone)), 
NOT(REGEX( Phone,"(\\D?[0-9]{3}\\D?)[0-9]{3}-[0-9]{4}"))
)
Hope this helps! Please mark as best if it does

Thanks
Michael Sabel 9Michael Sabel 9
Thanks for trying. That didn't work. It doesn't fire.
SarvaniSarvani
Hi Michael,

Try this please:
AND( 
NOT(ISBLANK(Phone)), 
NOT(REGEX( Phone,"(\\D?[0-9]{3}\\D?)\\s[0-9]{3}-[0-9]{4}")) 
)
Thanks
Michael Sabel 9Michael Sabel 9
Thanks. Still not firing.

User-added image
SarvaniSarvani
Hi Michael,

Thats works fine for me. Please make sure for the current validation rule which is active. You need to give space in between first three and second three digits like (123)Space456-7891 like (123) 456-7891. Then it will work and will not trigger any error. If you want to get rid of that space validation use the formula in my first response.

Please make sure you do not have any other validation rules active for phone number validation.

Hope this helps!

Thanks
Michael Sabel 9Michael Sabel 9
The issue is that it's not triggering an error for mistyped phone numbers. It's not firing for anything I put in. I'm trying this in a brand new dev account, with no other validation rules or anything else running. There must be something I'm doing, somewhere, that's messing this up.
Michael Sabel 9Michael Sabel 9
Sarvani, 

Could this be the problem? 

I'm literally using this:
AND(
NOT(ISBLANK(Phone)),
NOT(REGEX( Phone,"(\\D?[0-9]{3}\\D?)\\s[0-9]{3}-[0-9]{4}"))

But, should I be putting the specific phone API field name for every single phone field on the Contact page?

So for "Work phone," the VR should look like this possibly?
AND(
NOT(ISBLANK(npe01__WorkPhone__c)),
NOT(REGEX( npe01__WorkPhone__c,"(\\D?[0-9]{3}\\D?)\\s[0-9]{3}-[0-9]{4}"))

And for "Other Phone," the VR should look like this possibly?
AND(
NOT(ISBLANK(OtherPhone)),
NOT(REGEX( OtherPhone,"(\\D?[0-9]{3}\\D?)\\s[0-9]{3}-[0-9]{4}"))

Thanks!
SarvaniSarvani
Hi Michael,

Yes, If you are trying to validate custom phone fileds or other phone fields in addition to Standard phone field which has (Phone) as API name you should use their API names for validations like you mentioned. (npe01__WorkPhone__c, OtherPhone)

Hope this will resolve your problem.

Thanks
Michael Sabel 9Michael Sabel 9
Sarvani,

That worked using this formula you provided:

AND(
NOT(ISBLANK(HomePhone)),
NOT(REGEX( HomePhone,"(\\D?[0-9]{3}\\D?)\\s[0-9]{3}-[0-9]{4}"))
)

So, right now, it is enforcing this phone number format: 999 999-9999

Can you revise the formula so that it will enforce this number format instead: (999) 999-9999
I just need those parenthesis to be around the first three numbers. Thanks.

We're almost there!

Thanks.
SarvaniSarvani
Michael,

Please try below for the fromat (999) 999-9999:
AND( 
NOT(ISBLANK(Phone)), 
NOT(REGEX(Phone, "\\([0-9]{3}\\) [0-9]{3}-[0-9]{4}")) 
)

Thanks
This was selected as the best answer
Michael Sabel 9Michael Sabel 9
It worked. Thanks so much.