Loading

Common REGEX Validation

Publiceringsdatum: Jul 18, 2024
Beskrivning

Tips for commonly requested field validation that uses the REGEX() function.

 

Lösning

Note: In the following table, "_" denotes an optional space (the REGEX will accept it with or without the space).
 

DescriptionFormatsValidation
American Phone Number (no extension)(999)_999-9999
+1_(999)_999-9999
!REGEX(Phone,"^((\\+1)?\\s?\\(\\d{3}\\)\\s?\\d{3}\\-\\d{4})?$")
American Phone Number (w/extension)The above formats, plus:
(999)_999-9999_x999…
(999)_999-9999_Ext._999…
(999)_999-9999_ext:_999…
(any length of extension)
Accepts : or . after ext or x, and accepts +1 in front of number.
!REGEX(Phone,"^((\\+1)?\\s?\\(\\d{3}\\)\\s?\\d{3}\\-\\d{4}(\\s?(x|([Ee]xt[\\.:]?\\s?))\\d+)?)?$")
Social Security Number999-99-9999!REGEX(Social_Security_Number__c,"^(\\d{3}\\-\\d{2}-\\d{4})?$"
American ZIP code (5 digit)12345!REGEX(PostalCode,"^\\d{5}?$")
American ZIP code (5 or 9 digit)12345
12345-6789
!REGEX(PostalCode,"^(\\d{5}(\\-\\d{4})?)?$")
Brazilian Phone Number (no extension)(99)_9999-9999
(99)_99999-9999
+55 (99)_9999-9999
+55 (99)_99999-9999
!REGEX(Phone,"^((\\+\\d{2}\\s)?\\(\\d{2}\\)\\s?\\d{4}\\d?\\-\\d{4})?$")
Brazilian Phone Number (w/Extension)The above formats, plus the following:
[phone like above] ramal: 999…
[phone like above] ramal 999…
!REGEX(Phone,"^((\\+\\d{2}\\s)?\\(\\d{2}\\)\\s?\\d{4}\\d?\\-\\d{4}(\\sramal:?\\s\\d+)?)?$")
Brazilian CNPJ (business tax Id)99.999.999/9999-99!REGEX(CNPJ__c,"^(\\d{2}\\.\\d{3}\\.\\d{3}/\\d{4}\\-\\d{2})?$")
Brazilian Post code (5 or 8 digit)99999-999
99999
!REGEX(PostalCode,"^(\\d{5}(\\-\\d{3})?)?$")
Brazilian CPF999.999.999-99!REGEX(CPF__c,"^(\\d{3}\\.\\d{3}\\.\\d{3}\\-\\d{2})?$")
Japanese Phone Number (no extension)

99-9999-9999
999-999-9999
9999-99-9999
9999-999-999

!REGEX(Phone,"^[0-9]{2,4}-[0-9]{2,4}-[0-9]{3,4}$")

Japanese ZIP code ("-" is required)

999-9999

!REGEX(PostalCode,"^[0-9]{3}-[0-9]{4}$")

Japanese ZIP code (with or without "-")

999-9999
9999999

!REGEX(PostalCode,"^([0-9]{3}-[0-9]{4})?$|^[0-9]{7}+$")

Title Case words and Names
Covers all accents listed in the regex, and enforces Title Case (First letter capitalized, the rest lowercase) except special words (in some languages), da, de, di, do, and the d' prefix.
Smith
John de Souza
Rio de Janeiro
Brasília
d'Onofrio
di Lorenço
!REGEX(FirstName, "^((^|\\s)(((([DdO]')|Mc|Mac)?[A-ZÃÁÂÀÄÇÉÈÊËÍÌÎÏÕÒÔÓÖÛÚÙÜ][a-zãáàâäçéèêëíìîïñõôóòöûúùü]*)|(da|de|di|do)))*$")
Prevent users to enter a line break in a text field that supports multiple lines. ¹
REGEX( ShippingStreet , '(.*\r?\n.*)*')
Allow for a maximum of three lines in a text field that supports multiple lines. ¹
REGEX( ShippingStreet , '(.*\r?\n.*){3,}')

 

  1. When editing a field, depending on how you edit, and depending on your browser, either just a Line Feed is inserted ( ' \n ' ) or a Carriage Return ( ' \r ' ) and a Line Feed ( ' \n ' ). Therefore we need to check for, no or one Carriage Return ( ' \r? ' ), followed by a Line Feed ( ' \n ' ).
Ytterligare resurser

For assistance with a REGEX formula not listed here, visit the Trailblazer Community.  

REGEX

Java RegEx Documentation

Knowledge-artikelnummer

000386675

 
Laddar
Salesforce Help | Article