Subsections


6.4 HTML form scripts

It is possible to specify script code that should be run when the user interacts with the electronic form. The most common language used for scripts is JavaScript (commonly known as JavaScript or JScript).

JavaScript code can be included as the content of a script element in the head of the HTML code, or the JavaScript can be in a separate file and just referred to via a script element.

This section does not provide a reference for the JavaScript language (see Section 6.5); it is only a guide to the use of some predefined scripts that are made available with this book.

6.4.1 Validation scripts

The file validate.js contains JavaScript code for performing simple checks on the values entered for text fields. These functions can be used by adding an onblur attribute to the element to be checked, so that the code is run whenever the user interacts with that element. Typically, these will only be necessary with text elements or textarea elements.

In all cases, these functions will open a dialog containing an error message if the check fails. The user must click on OK in the dialog in order to continue and the content of the offending element will be highlighted when control returns to the form (i.e., no other data can be entered until the element has a valid value).

notEmpty(this)
 
An error message will appear if the value is left blank. The code below shows an example of the use of this function to ensure that a name is entered into a text component:

Surname <text onblur="notEmpty(this)">

isInteger(this)
 
The value must not be blank and the value must be a whole number. It is valid to enter a value that starts with a number, but the value will be truncated to just the number in that case.

isIntegerRange(this, min, max)
 
The value must not be blank, the value must be a whole number, and the value must be between min and max.

isReal(this)
 
The value must not be blank and the value must be a number (can include a decimal point). It is valid to enter a value that starts with a number, but the value will be truncated to just the number in that case.

isRealRange(this, min, max)
 
The value must not be blank, the value must be a number, and the value must be between min and max.

hasLength(this, n)
 
The value must be n characters long.

hasIntegerLength(x, n)
 
The value must be a whole number and the value must have n digits.

matchesPattern(this, pattern)
 
The value is checked against the regular expression in pattern. See Section 11.9 and Chapter 13 for information about regular expressions. The regular expressions in JavaScript correspond to Perl-style regular expressions.


6.4.2 Submission scripts

The file validate.js contains an JavaScript function called validateAll(). This script function can be used to check all form elements that have onblur attributes just before the form is submitted. The effect is to force all validation checks to be performed when the form is submitted and submission will not proceed if any of the checks fail. This should be added as the onsubmit attribute of the form element as shown below:

<form onsubmit="return(validateAll())">

6.4.2.1 Local form submission

The file echo.js contains JavaScript functions for performing “local” submission of form data. This means that, when the submit button is clicked, the data is not sent to an external program for processing, but is saved within the browser instead.

In order to perform local submission, the saveform() function should be added to the onsubmit attribute of the form element as shown below:

<form onsubmit="return(saveform())">

In addition, an extra button should be added to the form using the code shown below (the echoform() function is provided within echo.js):

<button onclick="echoform()">export</button>

When this button is clicked, a new browser window will be opened containing all of the data submitted so far. The data can then be copied and pasted elsewhere for permanent storage.

Navigation away from the form page (or closing the browser), without clicking the export button, will result in the loss of all of the submitted form data.

The saveform() function calls the validateAll() function from validate.js to perform validation checks before saving the form data within the browser.

6.4.2.2 Local form submission parameters

The functions in echo.js behave differently, depending on the value of certain parameters. For example, the format in which the form data is stored is controlled by a parameter called echoFormFormat. The parameters all have default values, but new values can be specified by including hidden elements in the HTML form (see Section 6.2.2).

An example is shown on line 51 of Figure 6.2 (reproduced below):

<input type="hidden" id="echoFormFormat" value="html">

This code sets the echoFormFormat parameter to the value "html", which means that the form data are stored as an HTML table. This parameter can also take the value "text", in which case the data are stored in a plain text format (see Section 7.5).

Other possible parameters and their values are:

echoFormHeaders
 
This can take the values true (the default) or false. If it is true then variable names (taken from the name attributes of the form elements) are stored with the form data.

echoFormFieldSep
 
If the format for saving data is plain text, this parameter controls what sort of character is placed between fields. The default is to use a comma.

echoFormQuote
 
This can take the values true (the default) or false. If the format for saving data is plain text, and this parameter is true, then any data values containing the echoFormFieldSep are quoted (a double-quote character, ", is placed at either end of the data value). Any double-quote characters are also replaced with a double double-quote.

echoFormReset
 
This can take the values true or false (the default). If it is true, then all form components are reset to their default values after the form is submitted.

The default values for these parameters mean that the default plain text format is a CSV format (see Section 7.5.5).

Paul Murrell

Creative Commons License
This document is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 License.