Toggle menu
862
3.8K
30.2K
279.1K
Catglobe Wiki
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Force number of characters: Difference between revisions

From Catglobe Wiki
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 ==...
 
Cg van (talk | contribs)
No edit summary
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
== Challenge ==  
== Force number of characters ==
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 ==  
In order to force the respondent to write a certain number of characters in a text sub question.
<source lang="javascript" line="1">
 
function jumpon()    
As a questionnaire creator
{    
 
       document["query"]["dir"].value = "next";
I want to force the respondent to write a zip code, or a phone number in a certain text field.
       document["query"].submit();
 
'''Example'''
 
[[Image:ValidateTextLength.JPG]]
 
=== Solution ===
 
*Add a Text grid question to Questionnaire editor like below
*Go to menu Properties -> Question scripts -> Java script tab -> Input script  
 
[[Image:ForceNoOfCharacter Code.jpg]]
 
=== Code ===
 
<source lang="javascript">
var normalQuestionCheck = questioncheck;
function extendedQuestionCheck()
{
  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>
=== Source ===
Questionnaire Resource Id on cg site: 159730

Latest revision as of 09:55, 12 January 2012

Force number of characters

In order to force the respondent to write a certain number of characters in a text sub question.

As a questionnaire creator

I want to force the respondent to write a zip code, or a phone number in a certain text field.

Example

Solution

  • Add a Text grid question to Questionnaire editor like below
  • Go to menu Properties -> Question scripts -> Java script tab -> Input script

Code

var normalQuestionCheck = questioncheck;
function extendedQuestionCheck()
{
   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;
}
questioncheck = extendedQuestionCheck;

Source

Questionnaire Resource Id on cg site: 159730