Force number of characters

From Catglobe Wiki
Revision as of 10:55, 12 January 2012 by Cg van (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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

ValidateTextLength.JPG

Solution

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

ForceNoOfCharacter Code.jpg

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