Difference between revisions of "Force number of characters"

From Catglobe Wiki
Jump to: navigation, search
(New page: == Challenge == You want to use automatically change question, but don't want to show the counter for the respondent. The Next button is often hided when this script is used. == Script ==...)
 
Line 1: Line 1:
 
== Challenge ==  
 
== Challenge ==  
You want to use automatically change question, but don't want to show the counter for the respondent. The Next button is often hided when this script is used.
+
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 ==
 +
[[Image:ValidateTextLength.JPG‎ ]]
 
== Script ==  
 
== Script ==  
 
<source lang="javascript" line="1">
 
<source lang="javascript" line="1">
function jumpon()    
+
var normalQuestionCheck = questioncheck;
{    
+
function extendedQuestionCheck()
       document["query"]["dir"].value = "next";
+
{
       document["query"].submit();
+
  var valid = normalQuestionCheck();
 +
  if (valid)
 +
  {
 +
       // var_a hold the respondents answer.
 +
      var var_a;
 +
      // This is where the index of your sub question is defines.
 +
      // The code below is that we use sub question has index 0
 +
      var_a = document["query"][quest.label + "." + 0].value;
 +
      if (var_a.length > 0)
 +
      {
 +
        if (var_a.length != 10)
 +
        {
 +
            alert('The field can only hold 10 characters.');
 +
            return false;
 +
        }
 +
      }
 +
  }
 +
  if (!valid)
 +
  {
 +
       alert('Please correct your answer.');
 +
      return false;
 +
  }
 +
  return true;
 
}
 
}
//defines how many milliseconds the question should be visible for the respondent
+
questioncheck = extendedQuestionCheck;
window.setTimeout("jumpon();", 1500);
 
 
</source>
 
</source>

Revision as of 10:54, 2 March 2009

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;