Difference between revisions of "Prioritize sub questions in a text grid question"

From Catglobe Wiki
Jump to: navigation, search
 
Line 1: Line 1:
==Challenge==
+
== Challenge ==
 +
 
 
In order to let the respondent prioritize a list of products
 
In order to let the respondent prioritize a list of products
  
Line 6: Line 7:
 
I want to allow him to enter only values in a list of priorities for each product (one priority is only used once)
 
I want to allow him to enter only values in a list of priorities for each product (one priority is only used once)
  
'''Example'''
+
'''Example'''<br/>[[File:PrioritizeSQInTextGrid.jpg]]
<br/>
+
 
[[Image:PrioritizeSQInTextGrid.jpg]]
+
== Solution ==
==Solution==
+
 
 
*Add a Text grid question to Questionnaire editor like below
 
*Add a Text grid question to Questionnaire editor like below
*Go to menu Properties -> Question scripts -> Java script tab -> Input script  
+
*Go to menu Properties -> Question scripts -> Java script tab -> Input script
[[Image:PrioritizeSQInTextGrid Code.jpg]]
+
 
==Code==
+
[[File:PrioritizeSQInTextGrid Code.jpg]]
<source lang=javascript>
+
 
 +
== Code ==
 +
 
 +
<source lang="javascript">
 
function isNumeric_LessThan6(sText)
 
function isNumeric_LessThan6(sText)
 
{
 
{
var ValidChars = "12345";
+
var ValidChars = "12345";
if (sText.length != 1)
+
if (sText.length != 1)
return false;
+
return false;
+
if (ValidChars.indexOf(sText) == -1)  
+
if (ValidChars.indexOf(sText) == -1)  
return false;  
+
return false;  
+
return true;
+
return true;
 
}
 
}
  
Line 31: Line 35:
 
function extendedQuestionCheck()
 
function extendedQuestionCheck()
 
{
 
{
var valid = normalQuestionCheck();
+
var valid = normalQuestionCheck();
if (!valid)
+
if (!valid)
return false;
+
return false;
+
var usedNumbers = "";
+
var usedNumbers = "";
ErrorMessages.getInstance().clearErrorMessages();
+
ErrorMessages.getInstance().clearErrorMessages();
var msg = "";
+
var msg = "";
+
$("input:text").each(
+
$("input:text").each(
function(i)
+
function(i)
{
+
{
if ($(this).attr("name").indexOf("QUESTION.") == 0)
+
if ($(this).attr("name").indexOf("QUESTION.") == 0)
{
+
{
//check the value
+
//check the value
var v = $(this).val();
+
var v = $(this).val();
if (!isNumeric_LessThan6(v))
+
if (!isNumeric_LessThan6(v))
{
+
{
msg = "Only 1-5 are allowed as input";
+
msg = "Only 1-5 are allowed as input";
valid = false;
+
valid = false;
return;
+
return;
}
+
}
+
if (usedNumbers.indexOf(v) != -1)
+
if (usedNumbers.indexOf(v) != -1)
{
+
{
valid = false;
+
valid = false;
msg = "One value can only be specified for one row";
+
msg = "One value can only be specified for one row";
return;
+
return;
}
+
}  
+
usedNumbers = usedNumbers + v;
+
usedNumbers = usedNumbers + v;
}
+
}
}
+
}
);
+
);
if (!valid)
+
if (!valid)
ErrorMessages.getInstance().showErrorMessage(msg);  
+
ErrorMessages.getInstance().showErrorMessage(msg);  
+
return valid;
+
return valid;
 
}
 
}
  
 
questioncheck = extendedQuestionCheck;
 
questioncheck = extendedQuestionCheck;
 
</source>
 
</source>
==Source==
+
 
 +
== Source ==
 +
 
 
Questionnaire Resource Id on cg site: 159730
 
Questionnaire Resource Id on cg site: 159730

Latest revision as of 04:17, 17 October 2013

Challenge

In order to let the respondent prioritize a list of products

As a questionnaire creator

I want to allow him to enter only values in a list of priorities for each product (one priority is only used once)

Example
PrioritizeSQInTextGrid.jpg

Solution

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

PrioritizeSQInTextGrid Code.jpg

Code

function isNumeric_LessThan6(sText)
{
 var ValidChars = "12345";
 if (sText.length != 1)
 return false;
 
 if (ValidChars.indexOf(sText) == -1) 
 return false; 
 
 return true;
}

var normalQuestionCheck = questioncheck;

function extendedQuestionCheck()
{
 var valid = normalQuestionCheck();
 if (!valid)
 return false;
 
 var usedNumbers = "";
 ErrorMessages.getInstance().clearErrorMessages();
 var msg = "";
 
 $("input:text").each(
 function(i)
 {
 if ($(this).attr("name").indexOf("QUESTION.") == 0)
 {
 //check the value
 var v = $(this).val();
 if (!isNumeric_LessThan6(v))
 {
 msg = "Only 1-5 are allowed as input";
 valid = false;
 return;
 }
 
 if (usedNumbers.indexOf(v) != -1)
 {
 valid = false;
 msg = "One value can only be specified for one row";
 return;
 } 
 
 usedNumbers = usedNumbers + v;
 }
 }
 );
 if (!valid)
 ErrorMessages.getInstance().showErrorMessage(msg); 
 
 return valid;
}

questioncheck = extendedQuestionCheck;

Source

Questionnaire Resource Id on cg site: 159730