Script API Tutorial

Numeric answer validation in «Open-ended» question

Validation means Respondent's answer verification.

Suppose that we ask a Respondent about his year of birth and use Open Ended question for it. Of course, we assume that the year indicated by Respondent will not be less than for example "1920", therefore we want to eliminate the possibility of receiving incorrect answers.

For that purpose we can write a script that will not allow the Respondent pass our survey in case he/she gives the answer deemed incorrect.

Give it a try

In order to reproduce this example, please undertake the following steps:

First, create an Open Ended question in Questionnaire with one Alternative.

Then, open Cool Tool Survey Script Editor and insert the following script into "OnValidate" tab (see the image below).

  • var answers = q['QuestCode'].get();
  • var respAnswer = answers[0];
  • if (respAnswer==null) {
  • return "MessageIfNoAnswer";
  • }
  • var variablename = respAnswer.value;
  • if (isNaN(variablename)){
  • return "MessageIfNotANumber"
  • }
  • if (variablename<ConditionValue) {
  • return "Message If wrong";
  • }

Then modify the tokens highlighted in red as described in the table below:

QuestCode The code of question
MessageIfNoAnswer Message that appears if Respondent doesn't answer the question. For example, it can be "Answer is required"
variablename Variable that contains Respondent’s answer. You can name it the way you like. For example, in our case ’birthYear’ will be fine. If you’re interested in Respondent’s region and want to validate it, the name can be 'respondentRegion'
MessageIfNotANumber Message that appears when Respondent enters characters, symbols etc. instead of digits (for indicating his year of birth)
ConditionValue Value that will be compared with Respondent’s answer. In our example 'ConditionValue' is '1920'. However, you are free to change it.
MessageIfWrong Message that appears if Respondent’s reply is less than our condition value. "Year can’t be less than 1920!" would be fine for our example.

In case you want to check that the year indicated by Respondent does not exceed another condition (e.g. highest possible value), you can add one more condition to the script:

  • if (variablename>ConditionValue2) {
  • return “Message If wrong";
  • }

Advanced Explanation

Let us go through abovementioned example line by line:

var answers = q['QuestCode'].get();

//we declare a variable with a name "answer" and assign the array of Respondent’s answers to it

var respondentAnswer = answers[0];

//as we have only one Alternative in our Question we can safely take the first answer from the array and assign it to 'respondentAnswer' variable

    if (respondentAnswer==null) {
  • return "MessageIfNoAnswer";
  • }

//In this block of code we check whether 'respondentAnswer' variable is null, and if it is, we raise the error and return the corresponding message.

var variablename = respondentAnswer.value;

// 'respondentAnswer' is an object that represents the answer of Respondent. It has several parameters, including "value" parameter, which actually contains the reply given by Respondent. We create a variable 'birthYear' and assign Respondent's answer to it.

  • if (isNaN(variablename)) {
  • return "Message If not a number"
  • }

// Here we use 'isNaN' function of the JavaScript to check whether the 'variablename' is a number or not. 'isNaN' expects any variable as its argument and checks whether it is a number. If the argument is not a number it returns 'true'.

if (birthYear<1920)

//now we compare 'birthYear' with '1920' value. If result of the statement is true, we go inside 'if' statement

return "Message If wrong";

//In order to block further run of the survey our script in "OnValidate" tab should return some message. In our example this message is "Year can’t be less than 1920!";

Learn more at CoolTool knowledge base