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

Script for making a text grid numerical summing to a specific value

From Catglobe Wiki

Challenge

In order to validate the inputs of a text grid question
As a questionnaire creator
I want to check if the sum of all values equals to a specific value

Solution

  • Create a text-grid question, accept the numberical for sub question
  • Add the below script to that question

Code

var expectedTotal = 100;
function specialQuestionCheck()
{
   var msg = "";
   var val = 0;
   for(i = 0; i < quest.questions.length; i++)
   {
      var tmpVal = document["query"][quest.questions[i].label].value;
      if(tmpVal == "")
      {
         // NOPI
      }
      else if(isNaN(tmpVal))
      {
         msg += "- Angiv venligst et  tal\n";
      }
      else if (tmpVal<0 || tmpVal>100)
      {
         msg += "Værdier skal være fra 0 til 100";
      }
	  else
	  {
		val += parseFloat(tmpVal);
	  }
   }

   if(msg == "")
   {
      if(val == expectedTotal)
      {
         return true;
      }
      else
      {
         alert("Værdierne skal summe til "+ expectedTotal +" - nuværende sum: " + val);
         return false;
      }
   }
   else
   {
      alert("Følgende fejl opstod:\n"+msg);
      return false;
   }
   return false;
}

var defaultQuestionCheck = questioncheck;

function newGoNext()
{
   document["query"]["dir"].value = "next";
   if(defaultQuestionCheck())
      return specialQuestionCheck();
   else
      return false;
}

quest.next = newGoNext;

Source

Questionnaire Resource Id on cg.catglobe.com site: 164079 (Question: Q11_Script_for_making_a_text_grid_numerical_summin)