Why You Might Want to Create Custom Regex Sitecore Validation Rules
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.
Common Scenarios for Using Validation Rules
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.
- Email Addresses - This is used mainly for contact forms or in the footer, to ensure that the content authors enter the contact email address correctly.
- Phone Numbers - Super similar scenarios as email addresses, the main reason I use phone number validation to ensure all phone numbers follow the same format. (ex: XXX-XXX-XXXX or (XXX) XXX-XXXX)
- Social Media Handles - Since every platform has different formats for their handles, it's always a good idea to create a custom rule for each platform.
- Product SKUs/IDs - Same as phone numbers, it's important that all product SKUs follow the same format. I find a custom product SKU rule catches lots of simple typos.
- Token Values - Token values are a great way to give your authors the ability to write text, then have the
{token}
value be replaced by either a frontend calculation or a returned API value that's specific to the user. (ex: Make “{accountBalance}
” required)
How to Configure Custom Sitecore Regex Validation Rules
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.
Creating New Validation Rules
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 to Sitecore Fields
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.
Quick Copy/Paste Regex Examples for Sitecore Validation Rules
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 |