Difference between revisions of "Prioritize sub questions in a text grid question"
(New page: ==Challenge== In order to let the respondent prioritize a list of products<br/> As a questionnaire creator<br/> I want to allow him to enter only values in a list of priorities for each pr...) |
|||
(One intermediate revision by one other user not shown) | |||
Line 1: | Line 1: | ||
− | ==Challenge== | + | == Challenge == |
− | In order to let the respondent prioritize a list of products | + | |
− | As a questionnaire creator | + | In order to let the respondent prioritize a list of products |
− | I want to allow him to enter only values in a list of priorities for each product (one priority is only used once) | + | |
− | '''Example'''<br/> | + | As a questionnaire creator |
− | [[ | + | |
− | ==Code== | + | I want to allow him to enter only values in a list of priorities for each product (one priority is only used once) |
− | <source lang=javascript> | + | |
+ | '''Example'''<br/>[[File:PrioritizeSQInTextGrid.jpg]] | ||
+ | |||
+ | == Solution == | ||
+ | |||
+ | *Add a Text grid question to Questionnaire editor like below | ||
+ | *Go to menu Properties -> Question scripts -> Java script tab -> Input script | ||
+ | |||
+ | [[File:PrioritizeSQInTextGrid Code.jpg]] | ||
+ | |||
+ | == Code == | ||
+ | |||
+ | <source lang="javascript"> | ||
function isNumeric_LessThan6(sText) | function isNumeric_LessThan6(sText) | ||
{ | { | ||
− | + | var ValidChars = "12345"; | |
− | + | if (sText.length != 1) | |
− | + | return false; | |
− | + | ||
− | + | if (ValidChars.indexOf(sText) == -1) | |
− | + | return false; | |
− | + | ||
− | + | return true; | |
} | } | ||
Line 23: | Line 35: | ||
function extendedQuestionCheck() | 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; | questioncheck = extendedQuestionCheck; | ||
</source> | </source> | ||
+ | |||
+ | == Source == | ||
+ | |||
+ | Questionnaire Resource Id on cg site: 159730 |
Latest revision as of 04:17, 17 October 2013
Contents
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)
Solution
- Add a Text grid question to Questionnaire editor like below
- Go to menu Properties -> Question scripts -> Java script tab -> Input script
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