Tip: You can do this in modern surveys and the scripts are now available! For more information, see:
- How do I validate an Open End question to only have text (no numbers)? (Modern Surveys)
- How do I validate an Open End question to only have numbers (no text)? (Modern Surveys)
Note: Allow/Disallow Spaces does not include leading spaces before the text in an Open End question or for spaces after the input text.
This script allows you to restrict what a respondent can enter into an Open End question. Using this script, you can:
- Allow/Disallow Numbers
- Allow/Disallow Alphabet Characters
- Allow/Disallow Symbols
- Allow/Disallow Spaces
- Set a Minimum and Maximum Length
Detailed Steps
- Copy the script below.
- Select the open end question or a page containing that question in the Questionnaire Tree.
- In the Scripting section, click Edit Scripts.
- Paste the script into On Validate tab.
- Change Q_NAME to the name of your open end question. If you have more than one open end question on the same page enforcing the same restrictions, simply append those question names comma-separated. e.g. Q1, Q2, Q3
- Change NUMBERS to either true (allow numbers 0-9) or false (do not allow numbers) depending on your requirement.
- Change CHARACTERS to either true (allow english/latin alphabets) or false (do not allow any alphabets) depending on your requirement.
- Change SYMBOLS to either true (allow symbols such as %, $, &, *, etc.) or false (do not allow symbols) depending on your requirement.
- Change SPACES to either true (allow any white spaces such as a single space, tabbing, etc.) or false (do not allow any white space) depending on your requirement.
- Change MIN to the minimum number of characters required. If no minimum required, change this to -1.
- Change MAX to the maximum number of characters required. If no maximum required, change this to -1.
- Change ERROR_MESSAGE to a message you'd like to show when the restrictions are violated.
e.g. Please ensure your answer is in a valid format. - Click Save.
- Validate and test your survey.
/* Script Name: How do I restrict what respondents can type into an open end 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[] vqs = { Q_NAME }; String errorMsg = "ERROR_MESSAGE"; bool allow_numbers = NUMBERS; bool allow_characters = CHARACTERS; bool allow_special_characters = SYMBOLS; bool allow_spaces = SPACES; int min_length = MIN; int max_length = MAX; //SYSTEM CODE BELOW if (!allow_characters && !allow_numbers && !allow_special_characters && !allow_spaces) { return; } string regex = "^{0}[{1}{2}{3}{4}]{{{5},{6}}}$"; string chaRgx = allow_characters ? "a-zA-ZÀ-ÿ" : ""; string numRgx = allow_numbers ? "0-9" : ""; string spcRgx = allow_special_characters ? "\\W" : ""; string spaceNospc = allow_spaces && !allow_special_characters ? "\\s" : ""; string spcNospace = !allow_spaces && allow_special_characters ? "(?!(\\S*\\s+\\S*))": ""; string min = min_length > 0 ? min_length.ToString() : "0"; string max = max_length > 0 ? max_length.ToString() : ""; regex = String.Format(regex, spcNospace, chaRgx, numRgx, spcRgx, spaceNospc, min, max); ValidateStringFormat(errorMsg, regex, vqs);
More Information
Read this WebHelp article to learn more about Open End questions.
Comments
0 comments
Article is closed for comments.