You can apply advanced validation to check responses against certain “patterns” or “regular expressions” (RegEx) by using the form designer's regex() function, e.g. =regex(string value, string expression). This function returns result of applying the given regex expression to the given value


Regular expressions use a powerful syntax which you can use to build very complex patterns. See the cheat sheet attached to this article for a reference or try RegExr, a free online tool to help you build your own regular expressions.


Some examples of RegEx:


RegEx examples

Explanation

Successful input

 taken

Validates whether the response contains the string "taken".

Undertaken

 ^taken

Validates whether the response starts with the string "taken".

taken over

 ^taken$

Validates whether the response matches the string "taken" exactly.

taken

 ^[0-9]{4}$

Validates whether the response is exactly 4 digits in length.

0063

 ^[0-9]{4,10}$

Validates whether the response is a 4 to 10 digit number.

1234567890

 ^[vVbB][0-9]{6}$

Validates whether the response begins with either the letter 'V' or 'B' (upper or lower case) followed by a 6-digit number.

B123456

 ^[0-9]{2}-[0-9]{6,7}\s[a-zA-Z]\s[0-9]{2}$

Validates whether the response starts with two digits, a dash, and then six or seven digits; after the 6/7 string of digits there is always a space, then a letter then a space and finally two more digits; not case sensitive).

07-1234567 G 01

 ^(\+?27|0)[6-9][1-7][0-9]{7}$

Validates whether the response is a South African mobile number (including the leading international dialling code). 

0828091234



Example:


Adding validation to a field to ensure the mobile number that is entered is exactly 10 digits long.

  1. Create a new Text field to capture the participant's mobile number. (Note: the Text field type should be used instead of the Number field type for capturing numeric identifiers)
  2. Click on the Validation tab.
  3. Click the 'Configure validation rules' button (if no rules have been set up yet).
  4. Select that the response will only be valid when 'all' of the criteria are met.
  5. Select that the response must be 'equal to' the criteria.
  6. Use the regex function to specify the regular expression rules: The first parameter is a reference to the current field ("$Text_field_3") as the 'value', and the second parameter contains the regular expression to evaluate the value against, in this case "^[0-9]{10}$".
  7. Additional validation criteria can be added as needed below.





Hint: Test your validation rules by using the 'Previewbutton on the far right in your top toolbar.  




Using the legacy form designer? Have a look at the article on advanced validation in the legacy form designer.