Use Regex To Level Up Your Salesforce Validation Rules

Salesforce validation rules can use regex to enforce fine-grained text validation. With regex, we can apply case sensitivity, ensure a set of valid characters, and more.

What is Regex?

Regex, or regular expression, is “a string of text that allows you to create patterns that help match, locate, and manage text” ( Originating as a mathematical concept, regular expression is now commonly used in search engines, text editors, and word processors. This string of text, which looks like jibberish to the untrained eye, defines the pattern to compare against a target piece of text.

Using a syntax consisting of metacharacters (characters that have special meaning) and regular characters (literal values like ‘a,’ ‘b,’ etc.) regex can define a search pattern. For example, the expression [a-z] defines a pattern that matches only lowercase alpha characters from a-z. Both ‘a’ and ‘z’ are regular characters, and ‘–‘ is a metacharacter representing ‘between.’

How Do We Use It?

Salesforce provides a regex function that is extremely helpful when composing validation rules. The use of regex allows short, effective validation rules that are precise and would otherwise take much more effort to implement.

Here’s the formula syntax:

REGEX(text, regex_text)

Here are some common ways regex can be used:

Phone and email validation

Enforcing case-sensitive entries

  • Make sure the first character of a name is capitalized

Enforcing specific syntax on Identification Numbers

  • Validate SSN is delimited by the hyphen at the right intervals
  • Validate Driver’s License Number starts with the right prefix depending on state
  • Verify Zip Code is an acceptable format for the United States


Real Examples

Here are some examples of how you can apply regex in a validation rule:

Example 1: Regex to Validate SSN

REGEX(SSN__c, “[0-9]{3}-[0-9]{2}-[0-9]{4}”)

Here we have a custom field to store SSN (Reminder: Always ensure sensitive data is adequately protected). The function defines the pattern that we will use to evaluate the string of text. It is indicated that it will accept any number between 0-9, provided it occurs {3} times only, followed by a literal hyphen, followed by another set of numbers, this time occurring only {2} times, another regular character hyphen and finally another set of numbers not to exceed a length of {4}

Example 2: Applying Regex to Validate Postal Code Format

OR(BillingCountry = “USA”, BillingCountry = “US”),
NOT(REGEX(BillingPostalCode, “\\d{5}(-\\d{4})?”))

This validation rule enforces a format for US Postal Codes, specifying that it must fit within a 55555 or 55555-5555 format. If the specified pattern is not found, the validation rule returns your error to the user.


Regex can seem overwhelming. No one expects you to memorize the syntax by heart! There are some great tools out there to help.

Use RegExr to validate and test your regex. RegExr provides a great interface that breaks down the syntax of your regex and allows you to see how it reacts to your text string.

Also check out this syntax table ‘Cheat Sheet.’ 

As always, take a look at Salesforce Formula documentation


Author: Grant Ongstad, Senior Salesforce Consultant

EightCloud was instrumental in showing us all that Salesforce could help us achieve while also training our staff to make changes to Salesforce on our own in the future. They helped us achieve important strategic initiatives, stayed on their initial budget and were more than a pleasure to work with. We look forward to working with them more in the future!"

- Anna Summerlin, Gleneagles Group

Let us craft a custom solution that will meet your current and future Salesforce needs and make your life easier.


Rotate Your Device

Partners and friends, we are proud to share the new EightCloud brand and website with you. Our transformation reflects our commitment to helping you achieve Salesforce excellence through a focus on efficiency, expertise and genuine partnership.

We wish you the best and look forward to connecting with you soon!

Show me the new site!