HTML and CSS Reference
In-Depth Information
EXPLANATION ( CONTINUED )
3
The regular expression test() method will return true if a valid Social Security
number was entered and false if not.
4
If nothing was entered in the text box, the user will be alerted, focus will go to the
text field, and the form will not be submitted.
5
The onSubmit event handler will be triggered when the user clicks the submit but-
ton of line 7.
6
The input type is a text field that will hold up to 11 characters.
7
When the user clicks the submit button, the onSubmit event handler will be trig-
gered. It will call the okSocial() function to validate the Social Security number.
See Figure 17.45.
Figure 17.45 The user enters a valid Social Security number.
17.5.6 Checking for Valid Phone Numbers
A valid U.S. phone number has ten digits: an area code of three digits, followed by the
subscriber number of seven digits. There might be parentheses surrounding the area
code, and dashes or spaces separating the numbers in the subscriber number. With reg-
ular expressions you can test for any or all of these conditions and then, if necessary,
remove the extraneous characters, leaving just numbers. Example 17.44 demonstrates
how to validate a simple U.S. phone number.
EXAMPLE 17.44
<html>
<head><title>Validating Phone Numbers</title>
<script type="text/javascript">
function ok_Phone(phform){
1
var regex = /^\(?\d{3}\)?-?\s*\d{3}\s*-?\d{4}$/;
2
if(regex.test(phform.user_phone.value)) {
return true;
}
Continues
 
 
Search WWH ::




Custom Search