Validation rules

Validation rules can be used to limit or control the information users can enter in a Web form element field.

Types of validation rules

Required: Requires the user to input a value. If selected, you can optionally add a Required message to override the default message that will display if the element is empty.

Unique: entered values must be unique. If selected, you can optionally specify Unique per user and/or Unique per entity, and optionally add a Unique message to override the default message that will display if the element value is not unique according to the selections.

Counter: Limits entered value to a maximum value of characters or words.

Pattern: A regular expression that the entered value is checked against.

The pattern validation rule allows you to enter a regular expression (regex) that the element’s value should match, For example, it could define that the text must start with, end with or contain a certain sequence of letters (these are simple applications).

Note: Regular expressions are case sensitive.

Here are some example of regular expressions and what they do:

  • Example 1: .*water.*

    • This regex would match any of the following, because they all contain “water” somewhere in the contents:

      • @uwaterloo.ca

      • whitewater

      • I need a drink of water.

    • It would not match any of the following, because regular expressions are case sensitive:

      • @uWaterloo.ca

      • WhiteWater

      • I NEED A DRINK OF WATER!

  • Example 2: .*@uwaterloo.ca

    • This regex would match any of the following, because they all end with “@uwaterloo”, followed by any character (see “special characters” for the explanation why), followed by “ca”:

      • example@uwaterloo.ca

      • My email address is example@example.com, not example@uwaterloo.ca

      • example@uwaterlooXca

      • @uwaterloo.ca

    • It would not match any of the following, because we did not allow characters after “ca”

      • example@uwaterloo.cafoo

      • You can email me at example@uwaterloo.ca, or my friend at example@example.com

But, if we want to only match UWaterloo email addresses, we need to improve the regex. To do this we need to know about special characters.

Special characters

  • A period ‘.’ matches any character.

  • An asterisk '*' means that the preceding character definition is optional

  • A plus '+' means that the preceding character definition must be matched one or more times

  • To literally match a period used in a string of text, we need to add a backslash ‘\’ before the period. To ensure that there is text before the ‘@', we much change the the asterisk ‘*’ to a plus '+’.

.+@uwaterloo\.ca

  • This regex would match the first two entries in Example 2, but not the third or fourth.

Now, to ensure there is not text in front of the email address, we need to ensure the regex won’t match if there are spaces in front. (This is a very basic test - there are more advanced regular expressions to more fully vet email addresses.)

[^\s]+@uwaterloo.ca

  • Now the pattern will only match a piece of text that starts with characters that do not contain a space, and ends with “@uwaterloo.ca”. The definition ‘[^\s]’ refers to any character that is not some kind of space, and the plus '+' means there must be at least one.

This example demonstrates the importance of testing your regex against data that should and should not match.

Note: The backslash ‘\’ is needed before any special character you want to match. Regular expressions have many more special characters beyond the ones shown in this example.

Example 3: You would like users to enter a number that matches the pattern: 555-555-5555.

Enter this code in the Pattern regular expression field: [0-9]{3}-[0-9]{3}-[0-9]{4}

This example introduces character classes, which are indicated by the square brackets ‘[]‘.

  • ‘[0-9]’ matches any digit from 0-9.

  • ‘{3}’ means that the previous thing must appear exactly 3 times. 

  • This regular expression matches a telephone number (within the North American numbering plan).

Note: in WCMS 3, the ‘^’ (caret) that you might put at the beginning of the regex, matching the beginning of the text, and its opposite ‘$’ that you might put at the end of the regex, matching the end of the text, are not needed - the regex is evaluated as if they are present.

Adding a Regular expression to a Web form

  1. From the administration bar, select Workbench, and then select the My Dashboard.

  2. Navigate to the Forms list.

  3. Locate the form you want to configure and select Build from the Operations column.

    Screenshot of Forms list
  4. Select Edit under the Operations column next to the element you would like to add a Regular expression to. Alternatively, select Add element to create a new element. The Pattern validation rule is not applicable to every element type.

  5. In the Edit element window, navigate to the Form Validation panel, and select Pattern.

  6. Enter the Regular expression in the Pattern regular expression field.

  7. Optionally enter a custom error message in the Pattern message field.

    1. Note: Complex validation rules should use a custom Pattern message to better explain why user-entered information doesn’t pass validation.

       

  8. Select Save.

Additional resources for regular expressions