This script checks whether a UK postal code entered by a participant is valid. If the format is incorrect, an error message will appear. Here, "Open End" refers to the following question types:
- Short Answer
- Long Answer
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 the format of a UK postal code? (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. */ const pcode = response.getDataPoint("Q_NAME"); const pcodeValue = pcode.get(); var regexpcode = /^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9][A-Za-z]?))))\s?[0-9][A-Za-z]{2})$/; //If the inputString is NOT a match if (!regexpcode.test(pcodeValue)) { response.setError("Q_NAME", "Please provide a valid postal code"); }
Comments
0 comments
Article is closed for comments.