Survey requirements often have specific criteria for how a response can be correctly entered by respondents. For requirements more specific than merely requiring a question to be answered, use the validate
widget or inline tag. The validate
tag evaluates Boolean criteria for validation expressions when the page submits. Each validation expression that fails causes its associated message to appear at the top of the survey page. If the respondents do not meet the criteria in the validation expression, they cannot proceed past the page.
Syntax
Use the validate
and message
tags to define the condition and failure messages for response validation. These tags can be defined inline within a question or table, or standalone as a separate widget.
Tip! A validate
tag must be on the same page as the criteria it validates. If it is on the next page, the respondent cannot adjust their answer per the error message to proceed. Most research surveys do not allow back buttons; as such, proper placement and testing of a validation is essential!
validate: Boolean expression message: Message to display if validation condition not met
Validations can also be written in a single-line of code within a question or table using the validation
tag. This tag accepts a condition as its input, followed by the message
tag and message text in curly braces.
validation: [condition] {message: message text to display if condition not met }
Note that single-line validation statements with the validation
tag must be attached to a widget. In addition, multiple validation
tags may be attached to a single variable. See below for examples.
Tip! When creating tables that need to ensure specific table row or column conditions are met, such as a certain number of options are selected per table row, or that contradictory or illogical option combinations are avoided, instead use the row validation
and/or column validation
tags. For more information on how to use single-line or multi-line row and column validations, see Table row validation and column validation.
Additional Tags
Tag | Description |
---|---|
flag |
Label of widget where icon should appear. |
warn |
Tag which permits respondent to continue after one validation failure; defaults to 'n' (no), also accepts 'y' (yes). |
validations |
Tag that may be used inline with a widget to allow multiple Not to be confused with the |
Note: If the warn
or flag
tags are required for use with a validation statement, SPs should use the multiline validation approach.
Value
When creating validations that are attached to a widget, the [value]
placeholder can be used as shorthand in both validate conditions and messages.
[value]
Examples
Validate response against a previous question
The following example uses validation in T2 to ensure that one's responses in the table match the responses from the previous question (Q1). If the respondent answers that they have children in either (or both) age ranges, they must enter a quantity for the age range(s) they selected. If they leave a text field blank or enter '0' for one of the age ranges in which they indicated they have children, the validation message will be displayed, indicating there is a discrepancy, and their response in T2 should be amended.
T2 may also be written using the validation
tag, combining the text of the message
tag and the condition from the validate
tag into a single-line of code, as shown below. The condition is used as the input, followed by the message portion of the validation, surrounded in curly braces - { }
. Note that the validation
tag must be attached to a variable and cannot be used as a standalone widget.
With multiple validations inline
In this example, QFAVORITE2 has two separate validations. The conditions use the [value]
placeholder instead of writing out $QFAVORITE2
.
Inline string
The validate
(and validation
) tag can also evaluate string (text) responses. The example below uses the validate
tag to verify that the exact string entered is 'yellow'.
Warning
The following example uses a validation with the warn
tag. If '1' is not selected, a message will be displayed, asking them to make sure this is the answer they wanted. Unlike normal conditions related to using the validate
tag, the respondent can still submit another answer choice due to the inclusion of warn: y
.
For more information, see warn.
Perl validation for ranking table
Tip! The below widget and validation statement are not needed for programming a ranking table and are shown for educational purposes only. Instead, use type: rank
to create ranking questions.
This example illustrates using the Perl scalar
method and the checkrank
helper method with a foreach
contained in a heredoc to validate responses in a table. This validation ensures a unique number is entered in each row and that the ranking is made in order starting with number 1.
Validations with regular expressions
The validate
widget can be used to evaluate responses using regular expressions (a.k.a., "regex"). There are quite a few resources online to learn Perl's usage of regular expressions. Regular expressions are very powerful and can be extremely useful for parsing out a question's response. It is recommended that SPs have a strong understanding of Perl's usage of regular expressions before attempting to use them in validation expressions.
In the example below, Q4 must contain only a 6 digit number.
In the next example, Q5 must contain only letters or numbers and no other characters. The length of Q5 must be exactly 8 characters long.
Q7 requires that responses must be in US dollar format and contain both dollars and cents. A whole dollar response of, e.g., '99' will not be accepted.
Note: This particular kind of validation shown in Q7 can also be accomplished by instead using datatype: decimals 2
. If the respondent does not enter a value that includes a decimal with two places after it, an error message will be displayed to the respondent, asking them to "Please enter a number with exactly 2 decimal places."
Validating contact information
The following example contains a table in which respondents will enter their contact information and best times to be contacted. Validations are used to ensure that the respondent's first and last names contain only letters. Note that the decline to answer
(a.k.a., dta
) tag invokes its own built-in validations as well.
The above contact table can also be written as follows using validation
tags.
Comments
0 comments
Please sign in to leave a comment.