This script will validate an exclusive checkbox on a page with a Date question. A participant will have to either select the checkbox or fill in a date in order to continue, but both cannot be done.
A Multi Choice question will be used for the exclusive checkbox - this can be either standard or visual.
Example
You are asking participants to enter their date of birth, but would like to give them an opt-out Prefer not to answer checkbox below the Date question.
Detailed Steps
- Create a page in your survey.
- Create your Date question as required within the page.
- Create a Multi Choice question within the page and add the opt-out text as an answer option.
Using the example above, the answer option will be Prefer not to answer.
Remove the instruction text from the Multi Choice question. - Ensure that both the Date and Multi Choice questions are set to Not Required in the question properties.
- Copy the script below into Notepad or any other text editor (e.g. Notepad++, Textpad).
- Change Q_DATE to the name of your Date question.
- Change Q_OPT_OUT to the name of your Multi Choice 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 participants by hiding it and dragging it to the end of the questionnaire tree (anywhere after the completion point of your study).
- Add an answer option to ERR_MSG hidden question with the following text; Please insert one response only. Then update the precode to 1Resp.
Note: This error message will be seen if the participant selects the opt-out checkbox and also enters a date. 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 survey, 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 the page by doing the following:
- Select the 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.
Script
/* Script Name: How do I validate an exclusive checkbox on a page with a date question? 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. */ VerbatimQuestion vq = Q_DATE; ChoiceQuestion dk = Q_OPT_OUT ; /************ Do not edit below ************/ if ((vq.Value == null) && (dk.SelectedAnswer == null)) { if (IsATR) Set(dk, 1); else { ValidationErrors.Add("AnswerIsRequired"); //Default system error } return; } else if ((vq.Value != null) && (dk.SelectedAnswer != null)) { if (IsATR) Unset(dk, dk.Answers); else{ NewError(((Answer)ERR_MSG.FindAnswerByCode("1Resp")).Html); } return; }
More Information
For more information, see our webhelp topics:
Comments
0 comments
Article is closed for comments.