Scripting - Validation Scripting, JavaScript

Custom validation scripting code can be inserted to the Validation script box to prevent users from continuing, deciding what page to jump to, and for advanced question validation. The Validation script box is located on the right side of the screen when you select a Section break object. Press the JS/Perl button to toggle between JavaScript style and Perl style syntax. You can also view the Validation script box from the Validation tab of the advanced question options for Section break objects.

Custom JavaScript validation code is used in combination with the compiled CGIs. Custom Perl validation code is used in combination with the Perl CGIs.

All of the custom validation scripting code should be put into the projectname.val file.

Warning:

Do not use the Custom Validation Scripting and For multi-page forms, change the next page options at the same time. This will result in erroneous validation code.

Local Variables:

Local variables in the .val file start with an underscore. So, if you want to create a temporary variable use something like _count to represent the variable. Otherwise, the variable will become part of the survey and be passed from page to page. If this is done, a hidden field will need to be put in the form.

If... Else...:

if(condition)
{
[statements]
}
else
(line break needed- nothing on this line after the else is evaluated)
{
(line break needed)
[statements] (line break needed)
}
(space usually necessary)

Using Page Variables:

Write page variables to temp variables before evaluating with an operator. Check-all-that-apply questions field names are followed by an underscore and the response code

(_PAGEWARN =~ 'foo')
It is better to use: HAS
(_PAGEWARN HAS 'foo')

Evaluating field value of Check-all-that-apply, single choice, questions:
/fieldname/ eq ...

Page names:

Each page name can be founcd by looking at the Page break that creates the page. Each Page break has a Fieldname The Fieldname may be something like S1 (default for the first page in a multi-page form) or P49 for some other name. To make the page name, take the Fieldname in lowercase and just put page in front of it like this:

S1 becomes pages1
P49 becomes pagep49

Functions:

Use special functions in the validation file to perform tasks. Some of these functions include:

runReport

Process a .rep file and return the HTML code that it outputs.

sendMail

Send an email message to the specified recipient. Note: the EMAILSERVER parameter must be in the .ini file for sendMail to know what email sever to use.

ShowPage

Decide what page is shown next. The page name is going to be "page" then the name of the Section break that starts that page. For example, if the page I wanted to go to is named "P2", I would put in "pagep2". The last page of a multiple page survey does not check for the ShowPage; it will automatically submit.

timestamp

The timestamp() function return the current time in the format YYYYMMDDHHMMSS.

Examples:

Send and Email
_msgtext = runReport('project.rep','project2.rep');
sendMail('raosoft@raosoft.com','EZSurvey submissions',_msgtext);

Compound Conditional Validation
if(Q35 =='2')
{
if(/Q36/ ne '')
{_PAGEWARN += 'notpossible,';}
}

Check-all-that-apply Validation (Make sure that no more than 3 boxes are selected.)
var _COUNTQ1 = 0;
if (/Q1/ eq '1')
{_COUNTQ1 = _COUNTQ1 + 1;}
if (/Q1/ eq '2')
{_COUNTQ1 = _COUNTQ1 + 1;}
if (/Q1/ eq '3')
{_COUNTQ1 = _COUNTQ1 + 1;}
if (/Q1/ eq '4')
{_COUNTQ1 = _COUNTQ1 + 1;}
if (/Q1/ eq '5')
{_COUNTQ1 = _COUNTQ1 + 1;}


if(_COUNTQ1 > 3)
{_PAGEWARN += 'Q1,';}

Rank Order Validation (Make sure that all 6 boxes are selected.)
var _COUNTQ3 = 0;
if(/Q3/ has '1')
{_COUNTQ3 = _COUNTQ3 + 1;}
if (/Q3/ has '2')
{_COUNTQ3 = _COUNTQ3 + 1;}
if (/Q3/ has '3')
{_COUNTQ3 = _COUNTQ3 + 1;}
if (/Q3/ has '4')
{_COUNTQ3 = _COUNTQ3 + 1;}
if (/Q3/ has '5')
{_COUNTQ3 = _COUNTQ3 + 1;}
if (/Q3/ has '6')
{_COUNTQ3 = _COUNTQ3 + 1;}


if(_COUNTQ3 < 6)
{_PAGEWARN += 'Q3,';}

Create a password for going to the next page in the survey.
var _pass = 0;
if (PASSWD eq 'test1'){_pass = 1;}
if (PASSWD eq 'test2'){_pass = 1;}
if(_pass ne 1) {_PAGEWARN += 'PASSWD';}

Require that D1 have an answer and branch based off of the response to D1.
if (D1 eq '')
{_PAGEWARN += 'D1,';}
if (_PAGEWARN eq '')
{
if (/D1/ eq '1BPO') {showpage('pageddbpo');
}if (/D1/ eq '2CD') {showpage('pageddcd');
}if (_PAGESKIP eq '' && _PAGENEXT eq '') {showpage('pageddbpo');}}

See also...