[SalesForce] Validation Rule for Number only in Salesforce is throwing an error for (XXX) XXX-XXXX format

I have 3 phone fields in Salesforce Contact. I am supposed to prevent users from entering characters in the phone field.

I have used the below validation rule

( 
    Phone != null && 
    NOT(
        OR( 
            REGEX(Phone, "[0-9 ]+"), 
            REGEX(Phone, "\\+[0-9 ]+") 
        )
    )
 ) 

|| 

( 
    HomePhone != null && 
    NOT( 
        OR( 
            REGEX(HomePhone, "[0-9 ]+"), 
            REGEX(HomePhone, "\\+[0-9 ]+") 
        )
    )
)

|| 

( 
    MobilePhone != null && 
    NOT(
        OR( 
            REGEX(MobilePhone, "[0-9 ]+"), 
            REGEX(MobilePhone, "\\+[0-9 ]+") 
        )
    )
)

The issue that I am having is that, when I enter a 10 digit number in mobile field, it is automatically changing it to (XXX) XXX-XXXX format and as a result the validation error is being displayed as it contains characters. Is there any way to stop it.

Best Answer

  1. You can change your locale if possible to avoid the situation.

The format used for currency, dates, times, phone numbers, and names of people in Salesforce is determined by your Locale setting.

  1. If you do not want the parentheses-space-hyphen formatting ((800) 555-1212) for a 10- or 11-digit number, enter a “+” before the number. For example: +49 8178 94 07-0. And write your validation accordingly.

When you enter phone numbers in standard Phone fields, Salesforce preserves whichever phone number format you enter. Phone fields can accept a maximum of 40 digits in length entered via the Salesforce UI (that can not be done via the API).

Phone field automatic formatting based on User Locale

If your User Locale is set to "English (United States)" or "English (Canada)," Salesforce automatically does following via JavaScript.

Phone numbers containing 9 digits

Data will be saved as is without any formatting (Ex: 123456789)

Phone numbers containing 10 digits

  • Phone numbers beginning with 1: Data will be saved as is without any formatting. (Ex: 1234567890)
  • Phone numbers beginning with any other digit: Data will be saved in a format using parenthesis (Ex: (123) 456-7890)

Phone numbers containing 11 digits

  • Phone numbers beginning with 1: Data will be saved as a 10 digit number using parenthesis, removing the first digit (1). Ex: A phone number entered as 12345678901 will be saved as (234) 567-8901
  • Phone numbers beginning with any other digit: Data will be stored as is without any formatting:Ex: A phone number entered as 23456789012 will be saved as 23456789012.

TIP

  1. If you do not want the parentheses-space-hyphen formatting ((800) 555-1212) for a 10- or 11-digit number, enter a “+” before the number. For example: +49 8178 94 07-0.
  2. If your 10- and 11-digit phone numbers have Salesforce’s automatic format, you might need to enter the parentheses when specifying filter conditions. For example, Phone starts with (415)

Reference:-