Adding a Validation Pattern
Customize the regular expression based validation patterns available for text fields.
When creating a text field in Umbraco Forms, a validation pattern in the form of a regular expression can be applied. Default patterns can be removed or re-ordered, and custom ones created and added.
Provided patterns
Umbraco Forms ships with three patterns: number, email, and URL. The class names are Number
, Email
, and Url
respectively, and all are found in the Umbraco.Forms.Core.Providers.ValidationPatterns
namespace.
Creating a custom validation pattern
To create a custom format function, create a class that implements IValidationPattern
. You will need to initialize five properties:
Alias
- an alias that should be unique across the patterns and is typically camel-cased with no spaces.Name
- the name of the pattern that will be visible in the backoffice.LabelKey
- as an alternative to providing a name, a translation key can be provided. This will be used to look-up the name in the correct language for the backoffice user.Pattern
- the regular expression pattern.ReadOnly
- a flag indicating whether the pattern can be edited in the backoffice.
The following example shows the implementation of a pattern for a United Kingdom postcode (credit for the pattern to Mecanik at StackOverflow).
Registering the validation pattern
As with other provider types, the validation pattern needs to be registered. There are options to add, remove, and re-order patterns.
An example registration using the IUmbracoBuilder
is shown below:
Using the pattern
With the pattern registered it will be available for selection by editors in the backoffice when they create validation for fields supporting this feature.
![](https://docs.umbraco.com/~gitbook/image?url=https%3A%2F%2F4176286366-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FCjGdZc3v7BTcqZFEVbeM%252Fuploads%252Fgit-blob-e90ec8642dc47b19782794ef5e20c5ceb1f57c0b%252Fvalidation-pattern.png%3Falt%3Dmedia&width=768&dpr=4&quality=100&sign=88c19135&sv=1)
Last updated