This script will allow you to limit an other-specify answer option to a numeric value only, i.e. only numbers 0-9. No text values will be allowed. The numbers allowed will be whole numbers only (no decimals) and you will need to specify the range of numbers allowed.
Example
You have a single choice question that asks business owners if they have more than one branch of their business.
The response options are Yes and No and if they select Yes, they need to specify how many branches they own (between 2 and 50). The Yes option will be an other specify option, so respondents can type the number into the text box.
Detailed Steps
- Create your single or multi choice question as required, including the other-specify option.
- Copy the script below into Notepad or any other text editor (e.g. Notepad++, Textpad).
- Change QUESTION_NAME to the name of your single or multi choice question.
- Change LOCATION_OF_OTHER_SPECIFY to the location of the other specify answer. For example, if your other specify response 5th option, then the location would be 5.
Please note this is not the precode value, but the value of the order of the option, since the precode value can be different.
In the example above, this would be 1. - Change MINIMUM_VALUE to the minimum value you are allowing the respondent to type in. In the example above, this will be 2.
- Change MAXIMUM_VALUE to the maximum value you are allowing the respondent to type in. In the example above, this will be 50.
- 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).
- Add this answer option to ERR_MSG: "Please enter a value between x and y." and update x and y to the minimum/maximum range of numbers allowed. Update the precode to oth_num_err
- 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 question by doing the following:
- Select your question in the Questionnaire Tree.
- In the Scripting section of the question, click Edit Scripts.
- Paste the script into the On Validate tab.
- Click Save.
- Validate and test your survey.
/* Script Name: How do I restrict the other-specify field to a numeric response? 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) { //FIELDS YOU WILL NEED TO CHANGE ChoiceQuestion cq = QUESTION_NAME; int loc = LOCATION_OF_OTHER_SPECIFY; int min = MINIMUM_VALUE; int max = MAXIMUM_VALUE; //DO NOT MODIFY ANYTHING BELOW THIS LINE string st = GetOtherSpecify(cq,loc); System.Text.RegularExpressions.Regex codePC = new System.Text.RegularExpressions.Regex(@"^-?\d*?$"); Answer ans_other = (Answer)cq.GetOrderedAnswers()[loc-1]; if ( ans_other.Selected ){ if (!codePC.IsMatch(st)) { NewError(((Answer)ERR_MSG.FindAnswerByCode("oth_num_err")).Html); return; } else if(codePC.IsMatch(st)) { float num = (float)Convert.ToDouble(st); if(num<(float)min || num>(float)max) { NewError(((Answer)ERR_MSG.FindAnswerByCode("oth_num_err")).Html); return; } } } }
More Information
For more information, please see our WebHelp topic Choice questions.
Comments
0 comments
Article is closed for comments.