HTML and CSS Reference
In-Depth Information
EXAMPLE 17.37 ( CONTINUED )
4
if ( zip.value == ""){
alert("You must enter a zip code");
zip.focus();
return false;
}
return true;
}
</script>
</head>
<body>
<big>
<form name="ZipTest" action="/error" >
Enter your zip code:
<input type="text"
name="zipcode"
size=5 />
<input type="button"
value="Check zip"
5 onClick="if( ok_Zip(ZipTest.zipcode)) {
alert('Zip is valid.')}" />
<br /><input type="reset">
</form>
</big>
</body>
</html>
EXPLANATION
1
The function, called ok_Zip() , is defined to validate the zip code entered by the user.
2
The regular expression reads: Look for exactly five digits. The beginning of line
and end of line anchors ensure that there will not be any extraneous characters
before or after the five digits.
3
The regular expression test() method checks that the value entered by the user is
a valid zip code. If not, an alert dialog box will tell the user, focus will be returned
to the text box, and false will be returned.
4
If the user doesn't enter anything, an alert dialog box will appear, focus will be re-
turned to the text box, and false will be returned.
5
The onClick event is triggered when the user clicks the Check zip button. A Java-
Script statement to call the ok_Zip() function is assigned to the event. If the user en-
tered a valid zip code, the alert dialog box will pop up and say so. See Figure 17.39.
Search WWH ::




Custom Search