How to Create Custom Regex Validation for Your Sitecore Fields
Ensure your Sitecore authors always have the correct formatting when you utilize Sitecore’s Field Validations.
Start typing to search...
Ensure your Sitecore authors always have the correct formatting when you utilize Sitecore’s Field Validations.
Sitecore Validation Rules are a great safeguard against any types of inconsistencies in your content that could not only break front end functionality, but also reduce content consistency between your different pages. Validation Rules will allow you to ensure that your authors follow data formats, preventing common input mistakes and reducing the need to train your authors to ensure they always follow a specific format to prevent content not rendering correctly in the front end. Validation Rules can be applied through the Quick Action Bar for that immediate feedback, the Validate Button for the checking of the entire content piece, the Validator Bar for contextual warnings, or Workflow Validation for process enforcement. Applying the rules in these different places allows for multiple levels of protection against any problematic content from reaching production.
Validation is an amazing tool, but that doesn't mean you should use it on every single field. Sure, if a field is “Required” then use the Out-of-the-box “Required” Validation Rule, but you don't need to add a custom Validation Rule to every field. Custom Rules are more valuable when you need to maintain specific formatting; think character limits, date formats, or replacing text with an API response. Maintaining a balance between using custom Validation Rules and not using any can create guardrails that stop critical errors from entering production - while over using custom rules when they're not needed and overly restrict and frustrate your authors.
There are an unlimited amount of scenarios when using Sitecore’s Validation Rules would make sense, but here are some of the ones that I commonly use.
{token} value be replaced by either a frontend calculation or a returned API value that's specific to the user. (ex: Make “{accountBalance}” required)Creating custom Validation Rules is super simple, inside Sitecore open up the system/setting directory and find the Validation Rules folder. The full directory is /sitecore/system/Settings/Validation Rules. Here, you can organize your custom rules into the folders that make the most sense. For Example: If we wanted to create a new {token} item, I would open the Field Rules folder and create a new subdirectory called Tokens. Then I can store all my tokens inside that Folder.
Another good tip is to follow a naming convention on all of your new Validation Rules that explains what the Rule is from just the title. I usually enforce [Rule Category] - [Specific Validation] on my projects - So an example of this naming convention would be: Required Token - date. This is so you, or any future developers, can easily see what Validation Rules are being applied to each field at a glance.

You can easily create a new Validation Rule by right-clicking on any of the sub-folders inside Validation Rules. If you add a new folder, it might be a good idea to copy the insert options over to allow you to easily insert new Validation Rules - You can also duplicate existing Validation Rules instead of creating new ones, however you need to double-check that you correctly replace all the values from the old rule to match your new one. Generally it's better to duplicate if you are creating a new version of an already existing rule, and create a new Validation Rule if you're, as the name implies, creating a new rule.
Title & Description are the values that are shown to your content authors when the rule is broken, so set a value that is descriptive enough for them to understand the issue.

Type & Parameters are the where we will configure the Regex validation. Since we are looking to use Regex, we can set the Type to the built-in Sitecore Regex Validator: Sitecore.Data.Validators.FieldValidators.RegexValidator,Sitecore.Kernel. After defining the Validation type, we need to add the Regex pattern into the Parameters section. Since this scenario is just a text replace, the regex pattern we need is extremely simple: Pattern=\{date\}

Applying Validation Rules is the simplest step out of the process. You just need to go to the field you want to apply the rule to, and apply it to any of the corresponding Validation Rules option. Generally it's recommended to add the rule to all the options, however the validation will still work if you only apply it to one of the options.
If you're new to Sitecore, you can find the field values inside the Templates directory. Every company has their own way of organizing fields, but it's likely you'll find the field items inside the Project, Feature, or Foundation folders.

| Format | Regex Parameters | Use Case |
|---|---|---|
| Token | Pattern=\{token\} |
When you need to replace with a value calculated in the code |
Pattern=^[\w.%+-]+@[\w.-]+\.[A-Za-z]{2,}$ |
Validating email addresses in contact forms | |
| Phone | Pattern=^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$ |
Ensuring consistent phone number formatting |
| URL | Pattern=^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$ |
Validating properly formatted website URLs |
| Twitter Handle | Pattern=^@[A-Za-z0-9_]{1,15}$ |
Validating Twitter/X usernames |
| Date (MM/DD/YYYY) | Pattern=^(0[1-9]|1[0-2])\\/(0[1-9]|[12][0-9]|3[01])\\/\\d{4}$ |
Ensuring dates follow MM/DD/YYYY format |
| Numeric Only | Pattern=^\d+$ |
Restricting input to numbers only |
| Price | Pattern=^\$?\d+(\.\d{2})?$ |
Validating price formats with optional decimals |
| Alphanumeric | Pattern=^[a-zA-Z0-9]+$ |
Ensuring input contains only letters and numbers |
| Product SKU | Pattern=^[A-Z]{3}-\d{3}-[A-Z0-9]{3}$ |
Enforcing consistent product identifier format |
| Zip Code | Pattern=^\d{5}(-\d{4})?$ |
Validating US postal codes |
| Canadian Postal Code | Pattern=^[A-Za-z]\d[A-Za-z][ -]?\d[A-Za-z]\d$ |
Validating Canadian postal codes (ex: A1B 2C3) |
| YouTube Link | Pattern=^(https?:\\/\\/)?(www\\.)?(youtube\\.com|youtu\\.be)\\/.+$ |
Ensuring properly formatted YouTube links |
| Hex Color | Pattern=^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$ |
Validating hexadecimal color codes |
| IP Address | Pattern=^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$ |
Validating IP address format |