How to Use Regex for Validation Rule in Salesforce

In Salesforce, it is imperative that the users within an organization enter and share data that are concise and clear. This ensures that the data shared throughout the organization is uniform and relevant to their goals and objectives. To ensure the integrity and correctness of every data entered into the system, Salesforce has included a feature called Regex. This is useful in validating the entered data.

What is Regex?

In general computer programming, Regex stands for “regular expression”. It is a language developed to define search patterns. Regex uses a sequence of various characters to represent certain search patterns that the text had to match with. This function is often used in Find and Replace operations for text editors and word processors.

How does it relate to Salesforce?

In Salesforce, Regex is a function tool used to guide any user in creating data and entering it to the system in a proper format. It utilizes a string of letters, numbers and special characters used as a single code to build a validation rule. The validation rule determines the format in which the desired data should be entered into the app system.

Codes and a String of Various Characters

Regex is not merely a string of characters. You need to put these characters together in a certain way to create a correct syntax. Using Regex function can be a little complicated, since it is based on the Java regular expression syntax. Users who have background on Java, Perl syntax and POSIX standard can use the Salesforce Regex function easily and seamlessly.

Creating a Salesforce Validation Rule using Regex

Suppose you want to create a validation rule for the correct format of a credit card number. First, navigate to Steps > Build > Customize > Account. Select Validation Rule from the list. Enter the syntax formula, then save the validation rule.

Here is the formula for the Credit Card Number:

NOT( REGEX( Credit_Card_Number__c , “(((\\d{4}-){3}\\d{4})|\\d{16})?”))

For the above example, the Credit Card Number field can accept 16 digits of the numbers 0-9 with or without a dash. If using a dash, group the 16 digits in four digits per dash. If you fail to comply with such a number format, an error message will appear, prompting you to re-enter data in the right format.

You can utilize several formulas for different fields that you use in your respective apps.

Leave a Reply

Close Menu