Home > Survey Logic > (Survey Logic) Chapter 3 – Validation Rules

## (Survey Logic) Chapter 3 – Validation Rules

Back to Chapter 2 – Entrance (Skip) Rules

Validation rules control whether the answer for a specific answer is valid or not, and if it is not valid – what message should be shown to the surveyor. Validation rules are not necessary for simple validation of questions; rather they are used in cases where more advanced validation is needed. For example, configuring the max and min allowed values for a numeric question is not something that you need a validation rule for but can be set through the properties of the answer. What do you need a validation rule for then? Validation rules are necessary when doing more sophisticated logical validations that span questions. Take a look at the samples below to learn more about situations where you need a validation rule for.

A question can have more than one validation rule set for it. Each rule contains both the actual rule expression and the message to show to the surveyor if that rule evaluates as true. When adding validation rules, you specify both the rule expression AND the message to be shown.

Once a question with a validation rule is shown to the surveyor and answered, SurveyToGo will evaluate all validation rules and if this question. If it finds a rule that evaluates to true, it will show the associated message and will not allow the surveyor to proceed to the next question. Validation rules are processed one by one in the order they are added.

Adding validation rules is done by:

1. Select the question you wish to add a validation rule for
2. Switch to the “Rules” tab
3. Click the “new” icon on the rules grid toolbar
4. Write the rule expression
5. Write the message you want to show to the surveyor if this rule evaluates to true

Sample validation rules

The following sections are sample snippets that will help you to perform the most common validation rules tasks.

(1) Validate that the answer to Q2 is lower than the answer to Q1. If not, show a custom error message

Survey Snippet

[Q1]: “How many persons are sitting in the car?” [numeric: min:1- max:8]

[Q1]: “Of those, how many are men?” [numeric: min:1- max:8]

Requirement

Validate that there are no more men in the car than the number of persons in the car. If this happens, show “You can’t specify more men than persons!” to the surveyor.

Solution

Add the following validation rule to Q2:

[message] “You can’t specify more men than persons!”

Explanation

The validation rule checks the answer to the current question (Q2) and compares it to the answer of question Q1. If the answer to Q2 is bigger than the answer to Q1, the error message is shown and the surveyor is not allowed to proceed until he fixes the invalid situation

(2) Validate consistency between 2 questions

Survey Snippet

[Q1]: “What type of smoker are you?” [light/medium/heavy]

[Q2]: “How many cigarettes do you smoke per day?” [numeric: min:1 max:50]

Requirement

A light smoker can’t smoke more than 10 cigarettes per day.

A Medium smoker can’t smoke less than 11 or more than 30 cigarettes per day.

A heavy smoker can’t smoke less than 31 cigarettes per day.

These 3 rules need to be validated as Q2 is a simple numeric question and a consistency problem can arise if a light smoker is chosen and 30 cigarettes per day are entered etc…

Solution

Add the following 3 validation rules to Q2:

RULE1

[message] “a light smoker can’t smoke more than 10 cigarettes per day!”

RULE2

[condition] Answer (1) == 2 && ( Answer (2) < 11 || Answer (2) > 30 )

[message] “a medium smoker can’t smoke less than 11 or more than 30 cigarettes per day!”

RULE3

[message] “a heave smoker can’t smoke less than 31 cigarettes per day!”

Explanation

The first validation rule makes sure that when a light smoker is selected in Q1, the number of cigarettes is no more than 10.

The second validation rule makes sure that when a medium smoker is selected, the number of cigarettes is at least 11 and no more than 30.

The third validation rule makes sure that when a heavy smoker is selected, the number of cigarettes is at least 31.

Go To Chapter 4 – Jump (Branch) Rules