How to Use REGEX in Screen Component Input Validations

How to Use REGEX in Screen Component Input Validations

Regular Expressions (REGEX) are powerful text matching patterns that help you validate strings with precision. In Salesforce, REGEX can be used in validation rules to ensure data follows a specific format, such as verifying email addresses, phone numbers, or ID structures. But that’s not the only place where REGEX can shine. You can also use REGEX in screen component input validations to validate user inputs before the flow continues.

In this post, we will explore how to use REGEX for input validations in Screen Flow and walk through practical examples to make your flows more reliable and user friendly.

How to Use The REGEX Function

The REGEX function in Salesforce checks whether a text value matches a specific pattern. It returns TRUE if the value fits the pattern and FALSE if it doesn’t. The syntax is simple:
REGEX(text, regex_text)
In this function, text is the user’s input, and regex_text is the regular expression pattern you want to validate against.

For example, you could use REGEX({!TextInput}, "^[A-Za-z ]+$") to ensure that text input contains only letters.

Explanation of each part:

  • ^ → means start of the string.
  • [A-Za-z ] → means any uppercase (A–Z) or lowercase (a–z) letter or empty space.
  • + → means "one or more occurrences" of the preceding pattern.
  • $ → means end of the string.

So together:

The text must start and end with only letters and must contain at least one letter (because of the +). If you replace + with *, it will mean zero or more letters, allowing even a blank value to pass.

For example "John" is a valid text because it contains letters only. However, "John123" is not valid because it contains numbers. Moreover, "" is not a valid input because it doesn't contain at least one letter (because of the +).

To control length, replace + with {min,max} (for example, {1,10}).

How to Use REGEX in Screen Component Input Validations

To add an input validation, open your screen component and go to the "Validate Input" section. There, you can define a validation formula and a custom error message. Unlike standard Salesforce validation rules, this formula works the opposite way. It must return TRUE when the input is valid, not when it’s invalid.

Screen Component Input Validation

Here’s how the Validate Input section looks when using the previous REGEX function to allow only letters and spaces.

REGEX in Screen Component Input Validations

Limitations of REGEX in Screen Component Input Validations

In the Spring '25 release, Salesforce introduced an update to Screen Component Input Validations. Previously, these validations were triggered only after clicking the Next button. Starting with Spring '25, validations now fire immediately as the user types or moves out of the input field, providing instant feedback.

However, there’s one limitation to note: if your validation formula uses the REGEX function, the validation will still trigger only after clicking the Next button, just like before.

In both Validation Rules and Flow Input Validations, Salesforce evaluates REGEX like this:

  • If the field is blank, REGEX returns TRUE, so the validation passes.
  • If the field has a value, then it checks whether the value matches your pattern.

That’s why using + doesn’t make the field required. REGEX only runs when there’s text to check.

Sample REGEX Functions for Screen Component Input Validation

Here are some common REGEX input validations you can use in Salesforce Screen Flow screen components, along with explanations and sample error messages for each pattern.

Only Letters (A–Z or a–z)

REGEX({!Input}, "^[A-Za-z]+$")

Explanation: Allows only letters, no numbers, spaces, or special characters.
Error Message: Only letters are allowed.

Letters and Spaces

REGEX({!Input}, "^[A-Za-z ]+$")

Explanation: Allows uppercase and lowercase letters as well as spaces — great for first and last names.
Error Message: Only letters and spaces are allowed.

Numbers Only

REGEX({!Input}, "^[0-9]+$")

Explanation: Ensures the field contains only digits, no letters or symbols.
Error Message: Only numbers are allowed.

Phone Number (10 digits)

REGEX({!Input}, "^[0-9]{10}$")

Explanation: Requires exactly 10 digits (no spaces, dashes, or parentheses).
Error Message: Enter a 10-digit phone number (numbers only).

Email Address

REGEX({!Input}, "^(?!.*\\.\\.)[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$")

Explanation: Validates standard email format. It must contain "@" and a domain (e.g., [email protected]). It also disallows consecutive dots anywhere.
(Note: The double backslash before the dot \. is needed in Flow to escape the special character.)
Error Message: Enter a valid email address.

Alphanumeric (Letters and Numbers)

REGEX({!Input}, "^[A-Za-z0-9]+$")

Explanation: Allows a combination of letters and numbers with no spaces or special characters.
Error Message: Only letters and numbers are allowed.

Allow Specific Special Characters (hyphen, underscore, period)

REGEX({!Input}, "^[A-Za-z0-9._-]+$")

Explanation: Allows letters (A–Z, a–z), numbers (0–9), and specific special characters: dot (.), underscore (_), and hyphen (-).
Error Message: Only letters, numbers, dots, hyphens, and underscores are allowed.

Limit Input Length to a Maximum of 10 Characters

REGEX({!Input}, "^[A-Za-z0-9._-]{1,10}$")

Explanation: Same as above, but limits the total length to a minimum of 1 and a maximum of 10 characters.
Error Message: Use only letters, numbers, dots, hyphens, or underscores up to 10 characters max.

Be the first to comment

Leave a Reply

Your email address will not be published.


*