Salesforce REGEX Examples to Enforce Data Entry Standards

Accuracy is a necessity when it comes to business databases. Ensuring accuracy starts with effective data entry standards.

REGEX() is a function you can use in Validation Rules to enforce specific data entry formats are followed. Through REGEX, you can set standards that ensure data is entered correctly.

Download a REGEX Rule Cheat Sheet

Below are some example functions to help you learn the cryptic format. You can use these REGEX examples or create your own to incorporate into your Salesforce Validation Rules.

Social Security Number: 999-99-9999
  • REGEX(SSN__c, “[0-9]{3}-[0-9]{2}-[0-9]{4}”)
  • REGEX(SSN__c, “\\d{3}-\\d{2}-\\d{4}”)
[0-9]{3} or
\\d{3}
Requires the numbers zero through nine to be entered 3 times
Requires a dash to be entered
[0-9]{2} or
\\d{2}
Requires the numbers zero through nine to be entered 2 times
Requires a dash to be entered
[0-9]{4} or
\\d{4}
Requires the numbers zero through nice to be entered 4 times
Phone Number:  (999) 999-9999
  • REGEX(Phone, “([1-9]{3}) \\[1-9]{3}-[0-9]{4}”)
  • REGEX(Phone, “(\\d{3}) \\d{3}-\\d{4}”)
( Requires an open bracket to be entered
[1-9]{3} or
\\d{3}
Requires the numbers one through nine to be entered 3 times or zero through nine to be entered 3 times
) Requires a close bracket to be entered
[1-9]{3} or
\\d{3}
Requires the numbers one through nine to be entered 3 times or zero through nine to be entered 3 times
Requires a dash to be entered
[0-9]{4} or
\\d{4}
Requires the numbers zero through nice to be entered 4 times
Postal Code:  99999 or 99999-9999
  • REGEX(BillingPostalCode, “\\d{5}(-\\d{4})?”)
\\d{5} Requires the numbers one through nine to be entered 5 times
(-\\d{4})? The “?” makes the items within the preceding brackets optional; in this case the dash and four additional digits
If you replace the ”?” with a “*”, the dash and four additional digits can be added multiple times.
IA Driver’s License:  999AA9999

REGEX( Drivers_License__c, “\\d{3}[A-Z]{2}\\d{4}”)

\\d{3} Requires the numbers one through nine to be entered 3 times
[A-Z]{2} Requires a capital letters A through Z to be entered 2 times

If you replace “[A-Z]” with “[a-z]”, it would require lower case letters.

If you replace “[A-Z]” with “[A-Z][a-z]”, it would allow both upper and lower case letters.

If you replace “[A-Z]” with “[C-F]”, then only upper case letters including and between C and F would be allowed.

\\d{4} Requires the numbers one through nine to be entered 4 times
Credit Card:  9999-9999-9999-9999 or 9999999999999999
  • REGEX( htc__Credit_Card_Number__c , “(((\\d{4}-){3}\\d{4})|\\d{16})?”)
 (\\d{4}-){3} Requires four digits followed by a dash to be entered 3 times.  This is the pattern of “9999-9999-9999-“
\\d{4} Requires four digits to be entered.  This is adding the last “9999” to the above string.  Pay close attention to the brackets
| Or
\\d{16} Requires 16 digits
? The ? makes one of the two format optional


Helpful Cheat Sheet – Get a REGEX Rule Cheat Sheet when you subscribe to my blog.

Want to learn more about how you can take advantage of REGEX validation rule to enforce data entry standards? Sign up for a free consultation and we can talk about your specific needs.

Terry Miller
Terry has spent over 20 years focused on business leadership and information technology. As an independent consultant, he enjoys working with a variety of customers to help them solve business problems using the Salesforce® platform. His ability to quickly identify bottlenecks and provide understandable solutions has gained him the trust of his customers. If you're looking for expert guidance on your next Salesforce® project, click here to contact Terry today.

Leave a Reply

Your email address will not be published.

*