This script will allow a participant to either answer each row of a Multi Choice Grid or select the check box option, but not both. The script works for both standard and visual questions.
Note: This validation will not work for ATR.
Example
You have a Multi Choice Grid that asks participants to select at least 1 statement associated with each of 5 companies. The statements are in the columns and the companies are in the rows.
You also want to give the participant a way to opt out of answering the question, so there is also a checkbox below the grid with the option "Prefer not to answer."
Detailed Steps
- Create a page in your survey.
- Create your Multi Choice Grid question (either standard or visual) as required within this page.
- In the properties section of the Grid question, deselect the Question completion required check box. Question completion will be enforced through the script.
- Create a new Multi Choice question (either standard or visual) on the page.
- In the Multi Choice question, create an answer option with the opt-out text. Using the example above, this should be "Prefer not to answer."
- In the Properties section of the Multi Choice question, deselect the Question completion required check box.
- Copy the script below into Notepad or any other text editor (e.g. Notepad++, Textpad).
- Change CHECKBOX_QNAME to the name of the exclusive Multi Choice question.
- Change GRIDNAME to the name of the Multi Choice Grid question.
- Set up the error messages (this method works across multiple languages):
- Create a single choice question and name it ERR_MSG. If this question already exists, do not create a new one.
- Ensure that ERR_MSG is not seen by respondents by hiding it and dragging it to the end of the questionnaire tree (anywhere after the completion point of your study).
- For the first error message, add this answer option to ERR_MSG: "Please select only one response." and update the precode to "one_resp_err".
- For the second error message, add this answer option to ERR_MSG: "Please complete the question." and update the precode to "no_resp_err".
- For the third error message, add this answer option to ERR_MSG: "Please fill out the entire grid before proceeding." and update the precode to "grid_fill_err".
Note: The script pipes in error messages from this ERR_MSG question, so you can update the error messages to your liking without affecting the script. If you have multiple languages in your study, simply translate the ERR_MSG question and the script will pipe in the error messages based on the current survey language.
- Insert the completed script into the Script section of your page by doing the following:
- Select your page in the Questionnaire Tree.
- In the Scripting section of the page, click Edit Scripts.
- Paste the script into the On Validate tab.
- Click Save.
- Validate and test your survey.
Note: The question names used in scripts are case-sensitive. Ensure that you use the exact question names as they appear in the Questionnaire tree of the survey.
Script
/* Script Name: How do I add an exclusive check box on a page with a multi choice grid? To ensure your script works as expected, always copy the original script from the script library found in the support portal. Failure to use the most up to date script from the script library may cause unexpected results. */ if (!IsATR){ ChoiceQuestion cq_multi = CHECKBOX_QNAME; GridSequence gs = GRIDNAME; /************ DO NOT MODIFY BELOW ************/ bool gridAnswered_any = false; bool gridAnswered_all = true; foreach (ShowQuestionAction sqa in gs.WorkflowSteps){ ChoiceQuestion cq = (ChoiceQuestion) sqa.ChoiceQuestion; if (cq.GetSelectedAnswers().Length > 0) gridAnswered_any = true; else gridAnswered_all = false; } if ((int) cq_multi>0 && gridAnswered_any) { NewError(((Answer)ERR_MSG.FindAnswerByCode("one_resp_err")).Html); return; } if ((int) cq_multi<0 && !gridAnswered_any) { NewError(((Answer)ERR_MSG.FindAnswerByCode("no_resp_err")).Html); return; } if ((int) cq_multi<0 && !gridAnswered_all) { NewError(((Answer)ERR_MSG.FindAnswerByCode("grid_fill_err")).Html); return; } }
More Information
Please click on the webhelp links below for more information:
Comments
0 comments
Article is closed for comments.