HTML and CSS Reference
In-Depth Information
EXAMPLE
17.44 (
CONTINUED
)
else{
alert("Enter a valid phone number");
return false;
}
}
</script>
</head>
<body>
<hr />
<h2>Checking for a Valid Phone Number </h2>
3
<form name="formtest"
action="http://localhost/cgi-bin/environ.pl"
method="post"
4
onSubmit="return ok_Phone(this);">
<p>
Please enter your phone: <br />
5
<input type="text" size=40 name="user_phone" />
</p>
<input type=submit value="Submit" />
<input type=reset value="Clear" />
</form>
</body>
</html>
EXPLANATION
1
The regular expression reads: Start at the beginning of the string, look for an option-
al literal opening parenthesis, followed by exactly three digits, and an optional clos-
ing parenthesis (the area code), followed by an optional dash, zero or more spaces,
exactly three digits, zero or more spaces, an optional dash, and ending in exactly
four digits, such as (222)-111-2345 or 222-111-2345 or 2221112345.
2
The regular expression is matched,
phform.user_phone.value
, the
test()
method
will return
true
, and the form will be submitted; otherwise, the user will be alerted
to enter a valid phone number.
3
The HTML form starts here and is named
formtest
.
4
The
onSubmit
event handler is assigned as an attribute of the
<form>
tag. It will be
activated when the user clicks the submit button. The handler,
ok_Phone
, passes
the form as an argument. The
this
keyword refers to the form named
formtest
and
returns a
true
or
false
value. If
true
, the form will be submitted.
5
The user will enter his or her phone number in a text field. See Figure 17.46.