HTML and CSS Reference
In-Depth Information
EXAMPLE 11.28
<html>
<head><title>Checking Email</title>
<script type="text/javascript">
1
function email(form){
// Validate the address
2
if( form.address.value.indexOf("@") != -1 &&
form.address.value.indexOf(".") != -1 ){
alert("OK address!");
3
return true;
}
else {
alert("Invalid address");
4
return false;
}
}
</script>
</head>
<body bgcolor="lightgreen">
<font face=verdana>
<b>
<div align="center">
5
< form name="mailchk"
action="http://cgi-bin/ml.pl"
method="post"
onSubmit="return email(this); ">
Enter your email address:
<p>
6
<input name="address"
type="text"
size=60 />
</p><p>
7
<input type=submit value="Check it out" />
<input type=reset />
</p>
</form>
</div></b></font>
</body>
</html>
EXPLANATION
1
A JavaScript function called email() is defined. It takes one parameter, a reference
to a form.
2
If the string method, indexOf , does not return a -1, then the @ character and a dot
(.) were found in the value entered by the user in the textbox, and an alert mes-
sage will let the user know his or her e-mail address is okay. This is where the val-
idation takes place.
3
If true is returned, the form will be submitted.
Search WWH ::




Custom Search