Search the knowledge base, browse our resources, and visit our forum for more detailed information
Last updated: 25 Nov 2025
Constraints, also known as validation criteria, are a type of form logic used to restrict the acceptable responses to a question based on a predefined condition. If the constraint condition is not met, a customizable error message is displayed, prompting the form user to enter a valid response.
To learn more about form logic in XLSForm, see Introduction to form logic in XLSForm.
This article covers the following topics:
Adding constraints to questions in XLSForm
Combining multiple constraint conditions
Customizing constraint error messages
Advanced constraints in XLSForm
Note: This article focuses on adding constraints in XLSForm. To learn about adding constraints in the KoboToolbox Formbuilder, see Adding validation criteria in the Formbuilder.
For hands-on practice with adding constraints in XLSForm, see KoboToolbox Academy’s XLSForm Fundamentals Course.
Constraints are built using question references, comparison operators, and constants. Constraint conditions must be met to validate or submit a form. Otherwise, an error message appears, and users are prevented from moving to the next page or submitting the form.
To add constraints in XLSForm:
Add a constraint column to the survey worksheet.
In the constraint column, define the condition that must be met for the response to be valid.
Use a period . to reference the question in the row where you are adding a constraint.
Use a comparison operator, followed by a reference value, to build a simple constraint.
For example, . > 18 restricts an integer question to accept only values greater than 18.
survey worksheet
type |
name |
label |
constraint |
|---|---|---|---|
integer |
age |
What is your age? |
. >= 18 |
integer |
household |
How many people live in your household? |
. <= 30 |
integer |
income |
Out of those, how many earn income? |
. <= ${household} |
survey |
The reference value in a constraint condition must match the type of the question for which you are adding a constraint. The reference value formats for main question types are listed below:
Question type |
Reference value format |
Example |
|---|---|---|
integer |
Number |
|
select_one |
Choice name (as defined in the choices worksheet) in quotation marks |
|
select_multiple |
Choice name combined with the |
|
date |
Date in the |
|
text |
Text in quotation marks (rarely used for constraints) |
|
To learn more about building form logic expressions in XLSForm, see Introduction to form logic in XLSForm.
Multiple constraint conditions can be combined into a single expression to determine whether a response is valid. Conditions can be combined using and, or, and not logical operators:
Use and when all conditions must be met for a response to be valid.
For example: . > 18 and . < 65
Use or when at least one condition must be met for a response to be valid.
For example: . < 18 or ${student} = ‘yes’
Use not to indicate that a condition or set of conditions must not be met (e.g., when two conditions cannot be true together for a response to be valid).
For example: not(. < 18 and ${household_head} = ‘yes’)
survey worksheet
type |
name |
label |
hint |
constraint |
|---|---|---|---|---|
integer |
age |
What is your age? |
Must be less than 18 or above 65 to participate |
|
integer |
weight |
How much do you weigh? |
Must be between 30 and 200 kg |
|
survey |
By default, when a response value in the form does not meet the constraint condition, a “Value not allowed” error message appears. It is recommended to customize this message to inform users why the value is invalid, allowing them to correct their input.
To customize the constraint error message:
Add a constraint_message column to the survey worksheet.
In the constraint_message column, enter the text you wish to display as the error message when the constraint conditions are not met.
survey worksheet
type |
name |
label |
constraint |
constraint_message |
|---|---|---|---|---|
integer |
age |
What is your age? |
. >= 18 |
Must be older than 18. |
Beyond basic constraints, you can customize conditions to ensure data quality and adapt to many data collection scenarios. To build more advanced constraint conditions in XLSForm:
Use parentheses to combine more than two conditions
Use functions for increased flexibility
Use regular expressions to restrict text responses
Examples of more advanced validation criteria include:
Criteria |
Description |
|---|---|
|
The response must be between 17 and 130 or be equal to 999 (often used for non-response) |
|
If the answer to |
|
The ‘none’ option cannot be selected if any other response in a |
|
The date entered must be before today’s date. |
|
The input is restricted to two numbers (using regular expressions). |
Did you find what you were looking for? Was the information clear? Was anything missing?
Share your feedback to help us improve this article!
KoboToolbox is maintained by Kobo Inc.