Use the validation
tag (alias validate
) to create custom restrictions, known as validations. The validation
tag checks a logical condition and prevents the respondent from proceeding to the next page if the condition is not met.
1. Pick two colors.
type: checkbox
randomize: y
1. red
2. orange
3. yellow
4. green
5. blue
6. indigo
7. violet
validation: countChecked($Q1)==2 {message: Please select exactly 2 answers.}
Details
- Validations consist of a condition and a
message
decorator. The condition is the evaluated logical statement, and the message communicates to the respondent why they cannot proceed. - The validation is triggered when the condition evaluates as false.
- Validations may be written as a tag on a question or table or as a standalone widget.
- Validations can accept checked functions, list functions, Perl, regular expressions, and more as input.
- The validation must be on the same page as the criteria it validates. If needed, add the
autopage: n
tag to prevent page breaks. - If a validation condition for a table needs to be repeated for every row or column of the table, the
row validation
orcolumn validation
tags may be more appropriate. -
Multiple
validation
tags may be added to a single question. - You can add the
warn: y
decorator to a validation to let respondents proceed after it has been triggered once, along with a clear message indicating they may continue.
Tip! To ensure a positive respondent experience, it's important to thoroughly test validations and write clear messages that explain what is needed for the respondent to proceed.
Additional examples
Creating a 'validation' widget
If attaching the validation
tag directly to a question does not work as expected, try creating a standalone widget. Place the validation
tag on its own line, separate from the referenced question or variable. The message
tag is required.
In the example below, if the respondent selects both "like" and "dislike" for sunshine, a validation message appears to confirm the response. A follow-up question is then generated with the why
tag, to gather more information. We also apply the warn: y
tag to let the respondent continue after the validation has been triggered once.
1. What do you <u>like</u> about running outdoors?
type: checkbox
randomize: y
1. Fresh air
2. Sunshine
3. Improve mental health
4. Variety of environments
5. It's free
2. What do you <u>dislike</u> about running outdoors?
type: checkbox
randomize: y
autopage: n
1. Mosquitos
2. Sunshine
3. Weather can impact the training
4. Have to purchase different shoes
5. Can be lonely
why: Why do you dislike the sunshine? {showif: anyChecked($Q2,2)}
validation: !(anyChecked($Q1,2) and anyChecked($Q2,2))
message: You said you both like and dislike sunshine when running outdoors. Are you sure? Please click the arrow button again to continue.
warn: y
The example above is for demonstration purposes only. In practice, the validation can be programmed in a single line of code.
Adding flags (icons) to a validation
When a validation is triggered, a warning icon appears next to the questions referenced in the condition. To add this icon to other questions on the page, include the flag
tag, and list the question IDs in a comma-separated format.
Using the validation
widget example from above, you could add also add a warning icon to the follow-up question triggered when the respondent selects "dislike" for Sunshine, as shown below.
validation: !(anyChecked($Q1,2) and anyChecked($Q2,2))
message: You said you both like and dislike sunshine when running outdoors. Are you sure? Please click the arrow button again to continue.
warn: y
flag: Q2_WHY
Using the 'value' placeholder
In validation conditions, the [value]
option data placeholder can be used instead of $QX
, where 'X' is the question ID where the validation has been added. Note that the [value]
placeholder is only available when the validation
tag is included on a question and not used as a standalone widget.
1. Pick two colors.
type: checkbox
randomize: y
1. red
2. orange
3. yellow
4. green
5. blue
6. indigo
7. violet
validation: countChecked([value])==2 {message: Please select exactly 2 answers.}
Validating a response against a previous question
The following example uses multiple validations on Q2 to ensure that the responses in the table align with those from the previous question (Q1). If the respondent indicates they have children in either or both age ranges at Q1, they must enter a quantity for the corresponding age range(s) at Q2.
1. Do you have children living in your home? type: checkbox exclusive: 99 1. 0-7 years old 2. 8-17 years old 99. No kids in those age ranges 2. How many kids do you have in the following age ranges? type: integer table showif: anyChecked($Q1,1,2) maxlen: 2 datatype: whole rowsfrom: Q1 validation: (anyChecked($Q1,1) and $Q2R1 > 0) or noneChecked($Q1,1) {message: In the previous question, you indicated you had a child between the ages of 0-7.} validation: (anyChecked($Q1,2) and $Q2R2 > 0) or noneChecked($Q1,2) {message: In the previous question, you indicated you had a child between the ages of 8-17.}
Validating text
The validation
tag can also evaluate text responses. Q4 in the example below requires the exact answer 'yellow' or 'Yellow' to continue.
4. What color is a banana? type: text maxlen: 6 validation: [value] eq 'yellow' or [value] eq 'Yellow' {message: Check your spelling.}
Verifying each table row has unique text
Use the listUnique
function in a validation to require respondents to enter a unique response for every row or column in a table.
In this example, the series
function is referenced as the list name, using [id]
as the option data placeholder to iterate through each option ID in the series. This constructs field references like '$Q6R1', '$Q6R2', and so on. The listUnique
function then checks whether any of the text entries in the rows are identical, and when identical matches are found, the validation is triggered.
6. Please list <b>three (3) <u>chocolate candy</u> brands</b> that you are aware of. type: text table rowsfrom: series[1..3] validation: listUnique(series[1..3]; $Q6R[id]) {message: Every brand must be unique.}
Validations with regular expressions
As with other tags and decorators, validations accept Perl syntax and regular expressions (a.k.a., "regex"). In the example below, regex is used to ensure that only letters are entered for the First Name and Last Name fields.
start table: 9 intro: Please enter your contact information to receive your incentive. type: text cellalign: left validation: ($Q9RFIRST =~ /^[a-zA-Z]+$/) {message: Please ensure there are only letters in your first name.} validation: ($Q9RLAST =~ /^[a-zA-Z]+$/) {message: Please ensure there are only letters in your last name.} FIRST. First Name LAST. Last Name EMAIL. Email datatype: email end tableNote that a validation for
datatype: email
isn't necessary; it's included automatically by the software.Implementing "if-else" statements in a Van Westendorp exercise
In each of the examples above, restrictions apply to all respondents at all times, regardless of their answer selection. However, if you need a conditional validation statement, you can use an "if-else" statement, as demonstrated in the Van Westendorp exercise below.
In this exercise, a block is used to send the respondent through three iterations ("loops") of the question, each asking for the price they'd pay for a customized solution.
- In loop 1, the validation ensures the price is within the "fair" range of $0-$1000.
- In loop 2, the price must be higher than the "fair price" but still below $1000 (an "expensive" price).
- In loop 3, the price must be greater than the "expensive" price but still under $1000 (a "prohibitively expensive" price).
The conditional statement applies if the respondent is in a specific loop. Then, if the answer doesn’t meet the condition, the validation is triggered.
Version Notice: This article covers features in our r9/IS Pro platform. If you're looking for information on this topic related to r8, see validate.
Comments
0 comments
Please sign in to leave a comment.