HTML and CSS Reference
EXAMPLE 17.36 ( CONTINUED )
<h2> Checking for Empty fields </h2>
<form name="formtest" action="/cgi-bin/form1.cgi" method="get"
Please enter your name: <br />
<input type="text" size=50 name="user_name" />
Please enter your phone number: <br />
<input type="text" size=30 name="user_phone" />
<input type=submit value="Send">
<input type=reset value="Clear">
A user-defined function called validate_text() is defined. It takes one parameter, a
reference to a form.
If the value in the first text field is an empty string (represents a string with no
text) or null (represents no value), the user is sent an alert asking him or her to
fill in his or her name. If a false value is returned, the form is not submitted.
If the value in the second text field is an empty string or null, the user is sent an
alert asking him or her to fill in a phone number. If a false value is returned, the
form is not submitted.
If both text boxes were filled out, a true value is returned, and the form will be
submitted to the server's CGI program whose URL is listed in the action attribute
of the form.
The onSubmit event is triggered when the user clicks the submit button. The han-
dler function, validate_text() , will be called with a reference to this form.
The input type for this form is a text box that will get the name of the user.
Another text box is created to hold the phone number of the user. See