JavaScript : Checking Form Content
Contents ]
Claude Levior

Address Validity Check

Checking that a field has a value is all very well, but making sure the input is actually valid is even better. An e-mail address always contains the character at '@' and a dot, and this is exactly what lines 18 to 27 check.

The instructions indexOf ('@',0) and indexOf ('.',0) return -1 if the user has forgotten the character "@" or "." in the address. If this is the case, a warning message is displayed and the field in question receives the focus.

Note the use of the double-pipe character (||) to introduce a choice (condition 1 or 2).

Syntax Reserved Characters If you want to include a JavaScript-reserved character in a string, such as the double-quotes as is the case in line 22, you just have to prefix it with a backslash.

In line 24, the select function automatically selects data in the e-mail address field when the user clicks on OK in the message dialogue box.

Once the user has correctly filled in the form, the else clause in line 28 is executed and the Verif function returns true. This value is immediately received by the onSubmit instruction of the form; this allows the data to be transmitted to the server.

A final word This unit has shown you how to check the Name and E-mail fields of a mail form, but the method can be applied to more complex forms to verify all kinds of data.




  1   2   3   4