# Entry Validation

## ☑️ What entry validation ?

Beside checking if a value has been entered in a field you may also setup more advanced rules using regular expression to make sure that the text entered by the respondent matches certain conditions.

You may also set the **maximum length of characters** allowed to be entered in your field.

![](https://1025048312-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M8fLhS0bmfBRyq0HdUm%2F-MBxJW6_rwxooZXjfBZJ%2F-MBxSXjb6_rt4xDgQCuU%2Fimage.png?alt=media\&token=8c2583e9-c6bb-412f-9f33-ff2e50b049b9)

{% hint style="info" %}
Advanced users may also create a custom field based [answer type](https://docs.ngsurvey.com/form-management/form-designer/answers/answer-types/creating-new-type) using  [custom javascript validation](https://docs.ngsurvey.com/form-management/form-designer/answers/answer-types/creating-new-type/custom-validation-code) to validate the respondent entries.
{% endhint %}

## 🔢 Regular expressions

A [regular expression](https://en.wikipedia.org/wiki/Regular_expression) (regex) is a string based search pattern that will check that the text entered by the respondent matches the expression pattern or not.&#x20;

![](https://1025048312-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M8fLhS0bmfBRyq0HdUm%2F-MBhEHQX1dJgLFMgYGxh%2F-MBhF_WSjl4w9Zy_E3R9%2Fimage.png?alt=media\&token=0717f3d7-23b5-428d-8ee6-d83a28f3a34c)

Using these patterns you check a value using patterns like emails, numbers, zip codes etc ... Almost any [field](https://docs.ngsurvey.com/form-management/form-designer/answers/answer-types/entry-field) based type can be validated against a regular expression created using the regular expression editor.

## ➕ Adding a regular expressions

To add a regular expression go to the [answer properties](https://docs.ngsurvey.com/form-management/form-designer/answers/answer-properties) page and click on the **+** icon

![](https://1025048312-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M8fLhS0bmfBRyq0HdUm%2F-MBxlrzsAiMipW8XigrE%2F-MBxmVt6uM9ItWMtBWkC%2Fimage.png?alt=media\&token=fe1c1c57-6254-48b7-a217-05d420ddf338)

{% hint style="info" %}
Regular expressions are only available to the user who created them. If you would like to share your regular expression with all the other ngSurvey users you may turn on its **built-in** property.
{% endhint %}

## 🔅 Regular expression properties

* **`Name`** is the display name of the regular expression.
* **`Regular expression`** is the actual regular expression pattern that will be used to match the respondent answer .
* **`Error message`** error message that will be shown to the respondent if its entry doesn't match the pattern.
* **`Built in`** let us share the regular expression with all the other users.

{% hint style="info" %}
You may find pre-made regular expression and test yours at  <https://regex101.com/>
{% endhint %}

## 🔢 Javascript

For field based answers you can set the Javascript validation and Validation message property on the [answer properties](https://docs.ngsurvey.com/form-management/form-designer/answers/answer-properties/field-properties) to use custom javascript code to validate your answer. \
\
In the code below respondentAnswerValue will be replaced by the actual value of the field. If the code returns false ngSurvey will block the submit or navigation and show the validation message.

```
return (respondentAnswerValue >= 18 && respondentAnswerValue <= 99);
```

In the code below we are using the getAnswer in the validation context to get a value from another answer in the survey. Here we are looking up an answer with a reporting alias set to "hh\_people".&#x20;

<pre><code><a data-footnote-ref href="#user-content-fn-1">re</a>turn (respondentAnswerValue &#x3C;= validationContext.getAnswer('hh_people'));
</code></pre>

The validation context support following method and properties

```typescript
export class ValidationContext {
   answer:Answer, // Current answer being validated
   question:Question, // Current question of the answer being validated
   questions:Question[], // All survey questions
   answers: Answer[];  // All survey answers

  public getAnswer(id: string): string // Get the value of an answer by looking up its reporting alias or id
  public isSelectedAnswer(id: string, value: string): boolean // Look up a question based on its id or reporting alias and check if the given value is being selected 
}

```

[^1]:
