HTML and CSS Reference
In-Depth Information
Figure 17.38 The user left the phone number field empty, so the form was not
submitted.
17.5.2 Checking for Numeric Zip Codes
If you ask the user for a five-digit zip code, it is easy to check using a regular expression
by matching for exactly five digits:
/^\d{5}$/
Here is another way to say the same thing:
/^[0-9][0-9][0-9][0-9][0-9]$/
Some longer zip codes contain a dash followed by four numbers. This long zip code
format could be represented as:
/^\d{5}-?\d{4}$/
The beginning and end of line anchors prevent the matched string from containing any
extraneous characters at either end of the string. See Example 17.37.
EXAMPLE 17.37
<html>
<head><title>Testing for a Valid Zip Code</title>
<script type="text/javascript">
1
function ok_Zip(zip){
2
var regex= /^\d{5}$/ ; / Match for 5 numbers
3
if ( regex.test(zip.value) == false) {
alert("Zip code must contain exactly five numbers!");
zip.focus();
return false;
}
Continues
 
 
Search WWH ::




Custom Search