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 |
^[a-zA-Z0-9]{10}$ | Validates whether the input is exactly 10 alphanumeric characters in length. | d147s05F2p |
^[A-Z][a-z]* | Validates whether the response starts with an uppercase letter and is followed by any number of lowercase letters. | Start |
^[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 |
^PID[0-9]{4}$ | Validates whether the response begins with PID (upper case) followed by a 4-digit number. | PID1234 |
^[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-9][0-9]{7}$ | Validates whether the response is a South African mobile number (including the optional leading international dialling code). | 0828091234 +27828091234 |
^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$ | Validates whether the input is a valid email address. | test-email@gmail.com |
^\d{4}\/\d{1,}\/\d{2}$ | Validates whether the input is a valid company registration number which has a format of 4 digits / 1+ digits / 2 digits. | 2000/123456/78 |
(((\d{2}((0[13578]|1[02])(0[1-9]|[12]\d|3[01])|(0[13456789]|1[012])(0[1-9]|[12]\d|30)|02(0[1-9]|1\d|2[0-8])))|([02468][048]|[13579][26])0229))(( |-)(\d{4})( |-)(\d{3})|(\d{7})) | Validates whether the input is a South African ID number. | 8908250143084 |
Hint: Test your validation rules by using the 'Preview' button on the far right in your top toolbar.
Examples:
Adding validation to a field to ensure the mobile number that is entered is exactly 10 digits long.
- 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)
- Click on the Validation tab.
- Click the 'Configure validation rules' button (if no rules have been set up yet).
- Select that the response will only be valid when 'all' of the criteria are met.
- Select that the response must be 'equal to' the criteria.
- Use the regex function to specify the regular expression rules:
- =regex($Text_field_3, "^[0-9]{10}$")
- The first parameter is a reference to the current field ("$Text_field_3") as the 'value'
- The second parameter contains the regular expression to evaluate the value against, in this case "^[0-9]{10}$"
- Additional validation criteria can be added as needed below.
Adding validation to a field to ensure that the participant identifier entered matches the required format - starting with the letters 'PID' and followed by 4 digits.
- After adding a field to capture the PID, click on the Validation tab and select 'Configure validation rules' as per steps 1-3 above.
- Select that the response will only be valid when 'all' of the criteria are met.
- Select that the response must be 'equal to' the criteria.
- Use the regex function to specify the regular expression:
- =regex($PID, "^PID[0-9]{4}$")
- The first parameter is a reference to the current field ($PID) as the 'value'
- The second parameter contains the regular expression to evaluate the value against, in this case"^PID[0-9]{4}$"