Print this page

Common REGEX Validation

Knowledge Article Number 000213868
Description The following table describes commonly-requested REGEX-based validation rules.
Resolution 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 CPF999.999.999-99!REGEX(CPF__c,"^(\\d{3}\\.\\d{3}\\.\\d{3}\\-\\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})?$")
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 which supports multiple lines. ¹
REGEX( ShippingStreet , '(.*\r?\n.*)*')
Allow for a maximum of three lines in a text field which 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 ' ).

See also:
Formula Operators and Functions I–Z : REGEX




promote demote