# Custom Validation Code

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

![](/files/-MBzaUDnPZFCjtoy1Zq4)

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: 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/form-management/form-designer/answers/answer-types/creating-new-type/custom-validation-code.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.
