Sub topic 9
Validating formats with REGEX
This chapter assumes you can write a basic validation rule and know AND, NOT, and ISBLANK. It builds on the blank-handling chapter, because REGEX behaves badly with empty values.
What REGEX does
REGEX(text, pattern) returns true when the whole text matches a regular expression. A regular expression is a compact pattern language for describing allowed characters and their order.
Two points matter in validation rules:
- A rule fires when its formula returns true. To block bad data, wrap the check in
NOT(...). - Salesforce requires the pattern to match the entire value, not just part of it. You don't need
^and$anchors.
Worked example: US postal codes
This rule flags a US billing postal code that is not five digits or ZIP+4 (12345 or 12345-6789).
AND(
BillingCountry = "USA",
NOT(ISBLANK(BillingPostalCode)),
NOT(REGEX(BillingPostalCode, "\\d{5}(-\\d{4})?"))
)Each line has a job:
BillingCountry = "USA"limits the check to US addresses, so other countries' formats aren't rejected.NOT(ISBLANK(...))skips empty values. Without it, a blank code fails the match and the rule fires. If you want the field required, enforce that in a separate rule.NOT(REGEX(...))is true when the format is wrong, which blocks the save.
Pattern building blocks
| Piece | Meaning | Example match | Column | Column |
|---|---|---|---|---|
| `\\d` | One digit | `7` | ||
| `[A-Z]` | One uppercase letter | `Q` | ||
| `{5}` | Exactly five of the previous item | `12345` | ||
| `{2,4}` | Two to four of the previous item | `abc` | ||
| `?` | Previous item is optional | `-6789` or nothing | ||
| `( ... )` | Group items together | `(-\\d{4})` | ||
| `\ | ` | Either side | `US\ | CA` |
The doubled backslash in \\d is intentional. The formula editor treats a backslash in a string as an escape character. If you prefer to avoid the issue, write [0-9] instead of \\d.
Common errors
- Forgetting the blank check. An empty field does not match most patterns, so
NOT(REGEX(...))returns true and users can't save records they never filled in. - Using REGEX on a non-text field. REGEX needs text. Wrap a number or other type in
TEXT()first, or compare the number directly. - Expecting partial matches. The pattern
\\d{5}rejects12345-6789, because the whole value must match. - Patterns that are too strict. Phone numbers, names, and postal codes vary by country. Rejecting valid input annoys users more than accepting odd input does.
- Ignoring case.
[A-Z]does not match lowercase. Use[A-Za-z]or accept both.
A note on cost
Complex patterns take more compile-time budget in the formula. Salesforce limits formula size (5,000 characters, and a compiled size limit). Keep patterns short and readable. If a pattern needs a paragraph of comments to explain it, consider a flow or Apex for that check instead.
Exercise
A custom text field PartNumber_c must look like three uppercase letters, a hyphen, then four digits (for example ABC-1234). Blank values are allowed. Write the error condition formula.
Show answer
AND( NOT(ISBLANK(PartNumberc)), NOT(REGEX(PartNumber__c, "[A-Z]{3}-\\d{4}")) ) The ISBLANK guard skips empty values, and NOT(REGEX(...)) is true when the format is wrong, so the save is blocked.
Exercise
A rule uses NOT(REGEX(Code__c, "\\d{5}")) and users report that "12345-6789" is rejected. Why, and how do you fix it?
Show answer
REGEX must match the entire value, and "12345-6789" has extra characters after the five digits. Change the pattern to "\\d{5}(-\\d{4})?" so the optional ZIP+4 suffix is allowed.