HTML and CSS Reference
In-Depth Information
EXAMPLE 17.43
<html>
<head><title>Testing for a Social Security Number</title>
<script type="text/javascript">
1
function okSocial(sform){
2
var regex=/^\d{3}-?\d\d-?\d{4}$/;
3
if ( regex.test(sform.ssn.value) == false) {
alert("Social Security number invalid!");
sform.ssn.focus();
return false;
}
4
if ( sform.ssn.value == ""){
alert("Please enter your Social Security number.");
sform.ssn.focus();
return false;
}
return true;
}
</script>
</head>
<body>
<big>
<div align="center">
<form name="snnTest"
method=post
action="/cgi-bin/testing"
5
onSubmit="return okSocial(this)" />
Enter your Social Security number: xxx-xx-xxxx
<p>
6
<input type="text"
name="ssn"
size=11 />
<p>
7
<input type="submit" value="Submit" />
<input type="reset" />
</form>
</big>
</div>
</body>
</html>
EXPLANATION
1
The function okSocial() is defined. Its purpose is to validate a Social Security
number.
2
The regular expression reads: Start at the beginning of the line, look for three dig-
its, one dash (or not one), two more digits, another possible dash, and ending in
four digits.
Search WWH ::




Custom Search