Custom JavaScript Verification
Top  Previous  Next

This is an advanced area, intended for those who know JavaScript.

Warning:
You must completely test your survey and custom JavaScript code to ensure that it functions properly and that the data for your entire survey are being saved correctly. Sawtooth Software does not support your custom JavaScript.

There might be times when our default question verification will not accomplish the functionality you want. Most questions (with the exception of the password, Terminate/Link, and conjoint questions) have a Custom JavaScript Verification section. This is available by editing the question, clicking on the "Advanced" button, and then selecting the "Custom JavaScript Verification" tab.

Custom JavaScript Verification allows you to specify custom verification for your questions. Your custom verification, written in JavaScript, is invoked when the respondent clicks the "Submit" button. You can configure your custom JavaScript to be called "before" or "after" system JavaScript verification.

Your respondents must have JavaScript enabled (most do) in order for your custom JavaScript to work.

javascript_verification  
In the example above, an error message is displayed and respondents cannot continue the survey if they have specified that they are older than their father. The age of the respondent and of the father are asked on the same page where this custom JavaScript is executed. The values of questions on the same page as the JavaScript that you are writing are available through the following JavaScript:

   document.mainform["QuestionName"].value

Notice in the example above, if everything is OK then the variable bln_custom_result is set to true. This tells the system that your custom JavaScript has verified that the respondent's data are OK. The page is ready to submit and the respondent can continue the survey.

If multiple questions on a single page all have custom JavaScript verification, then each section of custom JavaScript is run in order from top to bottom.

If you would like to retrieve information from previous pages you need to use SSI Script. Suppose for example that the respondent's age was submitted on a previous page. The JavaScript would then use SSI Script and look like this:

   if( [%QYourAge%] >= document.mainform["QFatherAge"].value)

The SSI Script executes on the server and the respondent's age (lets assume it is 37) is inserted. The resulting JavaScript is sent down to the browser and looks like this:

   if( 37 >= document.mainform["QFatherAge"].value)

You are responsible to ensure your custom JavaScript is working. The following two tips are highly recommended when trying to debug your custom JavaScript:

1)Turn on script error reporting. This is done in Internet Explorer by clicking Tools | Internet Options | Advanced. Under the Advanced tab go to the Browsing section and check Display a notification about every script error. This is extremely useful in alerting you to mistakes in your code.  
2)Use the "alert( )" function. If you are unsure what certain values are at certain points in your code, you can print them out to the screen by using the "alert( )" function. For example:  

      alert("My value at this point is: " + document.mainform["QuestionName"].value);

Note: Be careful about adding multiple variables that should be evaluated as numerical values using the "+" symbol. Before adding multiple variables, you'll need to specify that the variables should be treated as floating point values with the JavaScript parseFloat( ) function, placing each variable within the parentheses.

Example: parseFloat(document.mainform["q1"].value) + parseFloat(document.mainform["q2"].value)