Force number of characters

From Catglobe Wiki
Revision as of 11:54, 2 March 2009 by Catglobe (talk | contribs)
Jump to: navigation, search

Challenge

You want to force the respondent to write a certain number of characters in a text sub question. The script is often used together with the Number grid script, to force the respondent to write a zip code, or a phone number in a certain text field

Example

ValidateTextLength.JPG

Script

 1 var normalQuestionCheck = questioncheck;
 2 function extendedQuestionCheck()
 3 {
 4    var valid = normalQuestionCheck();
 5    if (valid)
 6    {
 7       // var_a hold the respondents answer.
 8       var var_a;
 9       // This is where the index of your sub question is defines.
10       // The code below is that we use sub question has index 0 
11       var_a = document["query"][quest.label + "." + 0].value;
12       if (var_a.length > 0)
13       {
14          if (var_a.length != 10)
15          {
16             alert('The field can only hold 10 characters.');
17             return false;
18          }
19       }
20    }
21    if (!valid)
22    {
23       alert('Please correct your answer.');
24       return false;
25    }
26    return true;
27 }
28 questioncheck = extendedQuestionCheck;