Desktop User Guides > Professional > Interview scripting > Writing interview scripts > Dealing with errors > Using a custom validation function > Writing the validation function
Writing the validation function
You define validation functions in the routing section of the script and specify which one to use for each question. Validation functions are defined like any other function and have the following basic structure:
Function Fname(Question, IOM, Attempt)
End Function
You can define functions anywhere in the script, but the usual place is at the end.
To specify which validation function to use for a question, place the statement:
Qname.Validation.Function = Fname
in the routing section somewhere before the statement that asks the question.
The function’s name. You can give your validation functions any names you like but it is a good idea to use names that explain what the function does.
mrScriptBasic statements.
The routing statements for the example script are as follows:
SelectBrand.Validation.Function = "ValidateBrands"

Function ValidateBrands(Question, IOM, Attempt)
' Check whether brands from more than one list have been selected
If (Question.Response.ContainsAny(Question.Categories[{BrandSetA}]) And _
Question.Response.ContainsAny(Question.Categories[{BrandSetB}])) _
' Add error message to the errors collection
Question.Errors.AddNew("SelectBrandError", IOM.Questions.SelectBrandError.Label)
' Set return value to False as validation has failed
ValidateBrands = False
ValidateBrands = True
End If
End Function
The script starts by naming the validation function to use. Then it asks the question and checks the responses chosen. If the response passes the standard validation tests, the interviewing program runs the validation function.
The If statement checks whether the respondent’s answer contains responses from both sections. It does not matter what the responses are; just that some are from one set and some are from the other set. If the test is True, the script adds the error message to the question’s Errors collection. The name of the message is SelectBrandError and its text is the text defined for that variable in the metadata section (stored in SelectBrandError.Label).
The function ends by setting a return value indicating whether or not validation was successful. If the return value is True the interview continues with the next question; if not, it redisplays the current question with the error message that was added to Question.Errors.
See also
Using a custom validation function