Forsta Plus provides several ways of validating survey responses. Some validation is based on the properties you define for your question, for example the field width on an Open Text Question.
Sometimes you need other types of validation on questions, or you want to specify your own error messages different from the error messages provided by the system. Enter your own validation code in the validation code field of the question's properties.
A validation code follows a pattern similar to this:
if(expression) { RaiseError(); <some function(s) setting the text of the error message(s)> }Expressions similar to those in conditions and column masks can also be placed in the validation code field, i.e. expressions that are either true or false. If the expression is true, the function:
RaiseError()is called. This function tells the interview engine that there is an error situation. This means that the interview page should be re-displayed, this time with one or more error messages. The respondent is thus prohibited from moving to the next page until the expression in the validation returns false.
Figure 1 - RaiseError function
There are several functions available to set the text of error messages:
ClearErrorMessage() |
takes away the error message at the top of the page (will remove the default error message). |
SetErrorMessage(LangID, message) |
defines the text of the error message at the top of the page (will replace the default error message). |
AppendErrorMessage(LangID, message) |
adds text to the current page's error message. |
ClearQuestionErrorMessage() |
takes away the error message for the current question (will remove the default question error message). |
SetQuestionErrorMessage(LangID, message) |
defines the text of the error message for the current question. |
AppendQuestionErrorMessage(LangID, message) |
adds message to the current question's error message. |
For LangID, insert a code to specify which language the error message is for. For example, the code
LangIDs.eninstructs the interview engine to use this error message when the language is English. These language codes, known as combidents, are listed in Appendix C.
Let us say you need to password-protect an open survey. Then you can start the survey with a question asking for a password that will be the same for all respondents. This can be set up as an open text question with the Password property, so that *s are displayed instead of the text the respondent writes.
To check that the password is correct, you can insert code similar to that shown below in the validation code field of the open question (in this example the question ID is password):
if(f("password") != "secret") { RaiseError(); SetErrorMessage(LangIDs.en,"Incorrect password. Please enter the correct password to participate in the survey."); }Figure 2 - Checking the password is correct