Lightning Flow: Use REGEX for Validation

What are Regular Expressions

The formulas in Salesforce give administrators a lot of flexibility in validating data. They help to ensure that each step of the business process is done correctly. But we can also use validation rules to check the input values. Even though each type of field has its own validation already in place, sometimes we need to limit it even more.That’s where the regular expressions, or in short, regex comes in.Regex is a sequence of characters that determines a string pattern. By using special expressions, operators and characters, regex can be used to control what kind of input is allowed.Regular expressions are especially useful when creating screen flows for external users. When you add a flow to a Site or Community to gather leads or feedback, you need to make sure that the data is valid. Also, it helps to prevent your automation from failing when a client decides to try the limits of your form.In this article, we’ll take a look at how to use Regex in a Lightning Flow.Let’s create a flow to collect leads from the web. This way, we can use regex for fields like phone number, and email address.

Regex Validation in Salesforce Screen Flow

Go to Flows in Setup, and create a Screen Flow.We’ll start by dragging and dropping a screen onto the canvas. No need to look up any of the records. All the information will be provided by the end-user.Let’s create a short form that requires basic details from customers.Put four Text components onto the screen, and label them: First Name, Last Name, Phone Number, Email Address.We don’t really need to validate First Name, or Last Name. Although we could ensure that they can’t be longer than a certain amount of characters. We won’t be needing REGEX for this yet. It can be done with a simple formula:LEN({!APIName_Of_Your_Field}) < 101Add this formula to the formula field in the “Validate Input” section of the component settings. This will restrict potential customers from inputting more than 100 characters in the field.

configure screen flow for using regex

But we are doing this to try out regex, so let’s get to more interesting fields in our form.Both “Phone” and “Email Address” have their specific components already available in the Flow Builder but it’s much easier to control the input in standard Text fields.Copy and paste the formula below in the appropriate field:REGEX({!Phone_Number},"[\s.()+0-9]{8,}")The formula probably doesn’t tell you much, so let’s break it into manageable chunks.First of all, the syntax for this formula is: REGEX(text, regex_text)In our case, the text is whatever is our field, and the regex text is the pattern we are limiting the input to. Here’s a short explanations what each character in regex means:[] - the brackets represent a set of characters that the text needs to match.- (dash) - indicates a range of characters\s - matches a white space{8,} - matches a token 8 or more timesThis REGEX isn’t really that limiting. It allows numbers, some specific characters ( .()+ ) and whiite spaces. Also the input has to be at least 8 characters long.If you expect customers from various countries (the format of their phone numbers may differ) this type of REGEX formula is perfect.But if you are only targeting clients from US, you could make it more specific:REGEX({!Phone_Number},"^((\+1)?\s?\(\d{3}\)\s?\d{3}\-\d{4})?$")What’s left is to configure the email address. This time, we will use the following REGEX formula:REGEX({!Email_Address},"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}")Just like before, we are using brackets to group characters. But this time, we’re adding a bit more control by ensuring that there are specific characters between them: “@” and “.”By doing this, the email can only be in a pattern of “text@text.text”.But you could go even further, e.g. limit the domain name to certain strings. We won't be needing that kind of control in this flow though.All that’s left is to create a new record by using the values from the screen, and we’re done. A regex-proofed form is ready to use.Depending on your business requirement, you might need some more validation formulas. Here are some other useful examples:Social Security Number:REGEX(Social_Security_Number__c,"^(\d{3}\-\d{2}-\d{4})?$"American ZIP code:REGEX(PostalCode,"^\d{5}?$")Visit this website for more examples of common REGEX validation. 


REGEX validation is one of the most useful formulas in Salesforce. You can use it to control input from internal and external users to ensure the data consistency, and limit errors in automations.Of course, this tutorial serves only as an introduction to regular expressions. If you want to regex in your processes, take a look at this guide. You can also practice and test your regular expressions using this site.For more articles on Flows and Salesforce, visit our blog.