HTML and CSS Reference
In-Depth Information
EXAMPLE 11.27 ( CONTINUED )
else {
9
return true;
}
}
</script>
</head>
<body bgcolor="lightgreen">
<font face=verdana>
<strong>
10
<form name="alphachk" onSubmit="return validate(this);">
Enter your first name:
<br />
11
<input name="first"
type="text"
size=60>
<p>
Enter your last name:
<br />
12
<input name="last"
type="text"
size=60>
<p>
13 <input type=submit value="Check it out">
<input type=reset>
</form>
</strong></font>
</body>
</html>
EXPLANATION
1
A JavaScript function called validate() is defined. It takes one parameter, a refer-
ence to a form object.
2
The if expression invokes a function, called alpha() , and passes the text object to
it. The first name is validated by the alpha() function. If false is returned, the block
is entered and the user is alerted that he or she did not enter a valid first name. If
this function returns false to the onSubmit handler that invoked it, on line 10, the
form will not be submitted.
3
As in line 2, the alpha() function is being called, only this time to verify the last
name of the user.
4
The function called alpha() is defined. All the validation work is done here. This
function will validate that the user entered something in the textbox, and that
what he or she entered is alphabetic characters, and only alphabetic characters,
either uppercase or lowercase.
5
If the length of characters entered in the text field is not equal to 0, then the block
is entered.
Search WWH ::




Custom Search