HTML and CSS Reference
In-Depth Information
EXAMPLE 17.36 ( CONTINUED )
else {
4
return true;
}
}
</script>
</head>
<body>
<hr />
<h2> Checking for Empty fields </h2>
5
<form name="formtest" action="/cgi-bin/form1.cgi" method="get"
onSubmit="return validate_text(formtest)">
Please enter your name: <br />
6
<input type="text" size=50 name="user_name" />
<p>
Please enter your phone number: <br />
7
<input type="text" size=30 name="user_phone" />
</p><p>
<input type=submit value="Send">
<input type=reset value="Clear">
</p>
</form>
</body>
</html>
EXPLANATION
1
A user-defined function called validate_text() is defined. It takes one parameter, a
reference to a form.
2
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.
3
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.
4
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.
5
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.
6
The input type for this form is a text box that will get the name of the user.
7
Another text box is created to hold the phone number of the user. See
Figure 17.38.
Search WWH ::




Custom Search