Validation patterns

Advanced Webform users may want to consider ways to force certain types of validation that aren't available by default in the feature, but can be accomplished using what are called 'regular expressions' or a 'regex'.

Definition: A regular expression or regex can be described as a special text string for defining a search pattern.

How to use a regex in Webform

Scenario: a user would like to force a guest to only provide an email address with the 'ucdavis.edu' domain. This isn't possible in any of the default options but can be done using a regex pattern.

  1. Create your form and add an email element to it.
  2. In your form's element list page, locate the email element and click its Edit button from under the Operations column.
  3. Scroll down and locate the Form Validation » Pattern field OR Element Attributes » Element custom attributes (YAML) field. The fields available will depend on the type of element; for example, text element will have a pattern field, but an email element will not.
    Webform regex on text fields
    Pattern field available from text field inputs.
    Regex field to use when applying a pattern to an email input
    Regex field to use when applying a pattern to an email input

     

  4. Enter your pattern. In this case, this pattern will achieve the desired requirement: 
    pattern: '^[\w\-\.\+]+@ucdavis\.edu$'.
  5. Scroll to the bottom and click Save to finish.
Learn more about regex patterns

Search your preferred tool for 'regex patterns' to find out how to write your own validation patterns.

Additional resources:

Tags