JavaScript : Checking Form Content
Claude Levior
Guard Policy
The
Verif function examines the content of our two fields. First (lines 4 to 9), it checks that the Name field has received a value.
If it is empty, i.e. the conditional expression is true, the instructions between the first pair of brackets are executed:
a dialogue box of the
Alert type is displayed.
The instruction in line 7 places the cursor in the empty field (the field receives the focus) so that the user can immediately
type a value in the appropriate field. This could be useful for a form with numerous fields since the user would not have
to search for the missed fields to fill them in.
At this point, the function returns false (line 8) and no data is sent to the server.
Raise the alarm! To space out a message displayed in an
Alert dialogue box, you can use the
n code which introduces a new line.
If the Name field has a value, the
Verif function proceeds to the second stage and checks the e-mail address field (lines 11 to 16).
The only difference compared with the first conditional block is that the instruction now references the second field by means
of
elements[1]; the index value for the first field was 0.