> For the complete documentation index, see [llms.txt](https://docs.ngsurvey.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.ngsurvey.com/form-management/form-designer/answers/answer-types/creating-new-type/custom-validation-code.md).

# Custom Validation Code

You may add custom javascript that gets executed on the go to validate the current respondent input.&#x20;

![](https://1025048312-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M8fLhS0bmfBRyq0HdUm%2F-MBzaEfZgfqpCU4Llkjj%2F-MBzaUDnPZFCjtoy1Zq4%2Fimage.png?alt=media\&token=804f058f-b665-4432-8567-b11ffdb34f94)

You may also cross check the values of other respondent answers in the survey using the **surveyAnswers** array containing all the answers to the questions that were answered by the respondent so far.

Your custom method must either return null if your check have passed or return an object with with a message property that will be shown to the user as an error message.

Here are basic sample of a method that checks that the respondent has entered something in the answer field.

```javascript
/* 
 respondentAnswerValue: value that was entered by the respondent 
 answer : answer object that is being validated
 question: question object to which the answer belongs
 surveyAnswers: all respondent answers posted so far  */

function isFilled(
respondentAnswerValue, //  :string 
answer,  //  : Answer
question, // : Question 
surveyAnswers // : SurveyFormGroupAnswer[]
) {
  if (!respondentAnswerValue || respondentAnswerValue.length == 0) {
      return { message : 'please enter something'};
   } 
   return null;
}
```

```javascript
export interface SurveyFormGroupAnswer {
  answer: Answer; // Answer to which this respondent answer belongs to
  question: Question; // Question of the answer
  answerControl: FormControl; // Form control that keeps the respondent answer value
  sectionIndex: number; // section index if the question is repetable.
  sectionId: string; // unique section id if the question is repeatable.
}
```

{% hint style="info" %}
The answer control is an [Angular form control](https://angular.io/api/forms/FormControl) object as such we can get its value using following notation answerControl.value.
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.ngsurvey.com/form-management/form-designer/answers/answer-types/creating-new-type/custom-validation-code.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
