This script checks whether an Open End question contains only numbers and no text. If the participant enters a response that contains text, an error message will appear.
Note:
-
This script will not work for a Number question. Here, "Open End" refers to the following question types:
- Short Answer
- Long Answer
- This script accepts whole number responses only. Decimal places are not supported.
Detailed Steps
- In your survey, add a Page element.
- Add an Open End question to the Page element.
-
Open the Page element for editing and click the Scripting button.
- Copy and paste the script below into the On Complete tab
- Replace Q_NAME with the name of your Open End question.
- Click Save.
- Preview and test your survey.
Script
/* Script Name: How do I validate an Open End question to only have numbers (no text)? (Modern Surveys) To ensure your script works as expected, always copy the original script from the Script Library found in the Alida Help Center. Failure to use the latest script from the script library may cause unexpected results. */ //declaring functions to test for only letters function onlyNumbers(str) { return /^[0-9]*$/.test(str); } //getting reference to the open end questions var vq = response.getDataPoint("Q_NAME"); //validating the entered text by running it through the declared functions above if(!onlyNumbers(vq.get())){ console.log("false"); response.setError("Q_NAME","Please provide a valid response of only numbers."); }
Comments
0 comments
Article is closed for comments.