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
Fried EggsFried Eggs 

Validation Rule for REGEX is triggering even if I type the phone number per the pattern

I need to right a validation rule that allows certain phone number patterns but when I follow the pattern the rule still gets an error. Here's the Validation rule:

AND(
NOT(ISBLANK( Phone )),
NOT(
OR(
REGEX( Phone , "\\s*\\(\\d{3}\\)_\\d{3}-\\d{4}\\s*$"),
REGEX( Phone , "\\s*\\+?1_\\(\\d{3}\\)_\\d{3}-\\d{4}\\s*$"),
REGEX( Phone , "\\s*\\(\\d{3}\\)_\\d{3}-\\d{4}_x\\d+\\s*$"),
REGEX( Phone , "\\s*\\(\\d{3}\\)_\\d{3}-\\d{4}_Ext\\.\\d+\\s*$"),
REGEX( Phone , "\\s*\\(\\d{3}\\)_\\d{3}-\\d{4}_ext:\\d+\\s*$")
)
)
)
 

Best Answer chosen by Fried Eggs
SubratSubrat (Salesforce Developers) 
Hello ,

Please try with the below validation rule :
AND(
    NOT(ISBLANK(Phone)),
    NOT(
        OR(
            REGEX(Phone, "\\s*\\(\\d{3}\\)\\s*\\d{3}-\\d{4}\\s*$"),
            REGEX(Phone, "\\s*\\+?1\\s*\\(\\d{3}\\)\\s*\\d{3}-\\d{4}\\s*$"),
            REGEX(Phone, "\\s*\\(\\d{3}\\)\\s*\\d{3}-\\d{4}\\s*x\\d+\\s*$"),
            REGEX(Phone, "\\s*\\(\\d{3}\\)\\s*\\d{3}-\\d{4}\\s*Ext\\.\\d+\\s*$"),
            REGEX(Phone, "\\s*\\(\\d{3}\\)\\s*\\d{3}-\\d{4}\\s*ext:\\d+\\s*$")
        )
    )
)

If this helps , please mark this as Best Answer.
Thank you.

All Answers

SubratSubrat (Salesforce Developers) 
Hello ,

Please try with the below validation rule :
AND(
    NOT(ISBLANK(Phone)),
    NOT(
        OR(
            REGEX(Phone, "\\s*\\(\\d{3}\\)\\s*\\d{3}-\\d{4}\\s*$"),
            REGEX(Phone, "\\s*\\+?1\\s*\\(\\d{3}\\)\\s*\\d{3}-\\d{4}\\s*$"),
            REGEX(Phone, "\\s*\\(\\d{3}\\)\\s*\\d{3}-\\d{4}\\s*x\\d+\\s*$"),
            REGEX(Phone, "\\s*\\(\\d{3}\\)\\s*\\d{3}-\\d{4}\\s*Ext\\.\\d+\\s*$"),
            REGEX(Phone, "\\s*\\(\\d{3}\\)\\s*\\d{3}-\\d{4}\\s*ext:\\d+\\s*$")
        )
    )
)

If this helps , please mark this as Best Answer.
Thank you.
This was selected as the best answer
Fried EggsFried Eggs
Thanks Subrat this worked beautifully.