HTML and CSS Reference
In-Depth Information
17.5.9 Putting It All Together
After writing the functions that validate each field of the form, they will be put together in
a single script to check all form entries. Example 17.48 combines just two of the functions,
to keep the example from being too large. One function, ok_Form() , calls the functions
that check individual entries; for example, ok_Email() checks for valid e-mail and returns
either true or false, and ok_Phone() checks for a valid phone number. After all of the entries
have been checked, the ok_Form() function returns either true or false to the onSubmit
event handler. If ok_Form() returns true , the form will be submitted to the server; if not, it
is stopped. If we add in all the credit card validation functions, this program will get really
large. Why don't you try it?
EXAMPLE 17.48
<html>
<head><title>Validating a Form</title>
<script type="text/javascript">
1
function ok_Email(emform){
2
var regex=
/^(([\-\w]+)\.?)+@(([\-\w]+)\.?)+\.[a-zA-Z]{2,4}$/;
3
if(regex.test(eform.user_email.value)){
return true;
}
else{
alert("Enter a valid email address");
return false;
}
}
4
function ok_Phone(phform){
5
var regex = /^\(?\d{3}\)?-?\s*\d{3}\s*-?\d{4}$/;
6
if(regex.test(phform.value)){
return true;
}
else{
return false;
}
}
7
function ok_Form(myform){
8
if (ok_Email(myform.user_email)== false){
9
alert( "Invalid email address");
10
myform.user_email.focus();
11
myform.user_email.select();
12
return false;
}
Continues
 
Search WWH ::




Custom Search