Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Current »

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 will be prompted to add an optional Required message that will display if the element is empty.

Unique: entered values must be unique. Optionally, you can specify Unique per user and/or Unique per entity.

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:

      • @uwaterloo.ca

      • whitewater

      • I need a drink of water.

  • Example 2: @uwaterloo.ca

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.

  • To literally match a period used in a string of text, we need to add a backslash ‘\’ before the period.

@uwaterloo\.ca

  • This regex would match the first two email addresses in Example 2, but not the third.

Now we need to ensure the regex only matches if it appears at the end of the text. For this, we add a dollar sign ‘$’ to the end. The dollar sign matches the end of the text.

@uwaterloo\.ca$

  • Now the pattern will only match a piece of text that ends with “@uwaterloo.ca”.

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. i.e. $ would match a dollar sign in the text, and not represent the end of the text.

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. 

  • ‘^’ (caret) matches the beginning of the text. It is the opposite of ‘$’ (which matches the end of the text).

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

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. Enter a custom error message in the Pattern message field.

    1. Note: Complex validation rules require a custom error message that will be displayed if user-entered information doesn’t pass validation.

  8. Select Save.

Additional resources for regular expressions

  • No labels