# Constraint

The constraint column is used to validate user input. It prevents the user from continuing until the answer meets a condition you define.

If the answer doesn't satisfy the constraint, a message defined in `constraint_message` is shown to the user.

## 📝 How constraints work

The value of the question is represented by a dot (`.`), which means “the current answer.” You build a logic expression using that value.

Constraints are commonly used to:

* Set numeric ranges (e.g., age must be between 0 and 120)
* Require matching formats (e.g., ZIP codes, emails)
* Enforce dependencies (e.g., end date after start date)

| type    | name  | label         | constraint                         | constraint\_message           |
| ------- | ----- | ------------- | ---------------------------------- | ----------------------------- |
| integer | age   | Your age      | . >= 18 and . <= 99                | Age must be between 18 and 99 |
| text    | zip   | ZIP code      | regex(., '^\d{5}$')                | Enter a 5-digit ZIP code      |
| text    | email | Email address | regex(., '^\[^@]+@\[^@]+.\[^@]+$') | Enter a valid email           |
| date    | end   | End date      | . >= ${start}                      | End date must be after start  |

## ☑️ Using the dot (.) in constraint

The dot `.` represents the value entered by the user for the current question. Use it inside functions and expressions:

* `. >= 0` — valid if value is 0 or higher
* `regex(., '^\d{5}$')` — valid if it matches the pattern

## 🔠 Variable references

You can use `${question_name}` to reference answers from other questions in the form. Make sure referenced questions appear earlier in the form.

Example:

* `. > ${min_age}` — ensures current input is greater than a previously entered value

## 🔢 Operators and functions for constraints

Same as relevance, you can use:

\=, !=, >, <, >=, <= and, or, not()

| Function        | Description                          | Example                                |
| --------------- | ------------------------------------ | -------------------------------------- |
| regex()         | Validates the format of a text input | `regex(., '^\d{5}$')` for US ZIP codes |
| string-length() | Checks the number of characters      | `string-length(.) <= 50`               |
| selected()      | Checks if a choice was selected      | `selected(${choices}, 'option1')`      |
| today()         | Gets the current date                | `. <= today()`                         |
| now()           | Gets current date and time           | `. <= now()`                           |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.ngsurvey.com/projects/import-export/xlsform/survey-sheet/constraint.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
