Friday, November 18, 2016

Salesforce: Regex allow only Numbers and Special Character

Phone field in Salesforce by default will accept any characters with maximum of 40 characters. Because of this flexibility, user can enter any data and cause the data become dirty.

Requirement: to allow only numbers, space, and special characters + ( ) . - / in phone, fax, mobile field.

Solution: use Regex in Validation Rules

NOT ISBLANK( Phone ) && 
NOT(REGEX( Phone, "^(?=.*?[1-9])[0-9()/\\-\\+\\s\\.]+$"))

Regular expression syntax is based on Java Platform SE 6 syntax. However, backslash characters (\) must be changed to double backslashes (\\) because backslash is an escape character in Salesforce.