Flic
10-17-2006, 04:14 PM
Hi,
I have a page with a form on it. I also have as part of the page some form validation using javascript. However, and this is rather frustrating, the form is being sent even when the validation is failing. If the form fails validation it shouldn't be sent.
The code is:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="geco.css" />
<link rel="stylesheet" type="text/css" href="geco_form.css" />
<SCRIPT type="text/javascript" language="Javascript">
function check(selected) {
var formok = false;
var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
if ((selected.name.value == "") || (selected.email.value == "") || (selected.enq.value == "")) {
formok = false;
if (selected.name.value == "") {
document.getElementById('namel').style.color = "#FF0000";
} if (selected.email.value == "") {
document.getElementById('emaill').style.color = "#FF0000";
} if (selected.enq.value == "") {
document.getElementById('enql').style.color = "#FF0000";
}
} else if (selected.email.value.match(emailExp)){
formok = false;
} else {
formok = true;
}
return formok;
}
</SCRIPT>
</head>
<body>
<div id="header"> </div>
<div id="links_bar">
<div class="links" id="nonclick"> </div>
<div class="links" id="click"><a href="index.html">About</a></div>
<div class="links" id="click"><a href="contact.html">Contact</a></div>
<div class="links" id="click"><a href="where.html">Where we are</a></div>
<div class="links" id="nonclick"> </div>
</div>
<div id="seperator"> </div>
<div id="content">
<h1>Email Us</h1>
<form name="contactform" method=post action="send.php" onsubmit="return check(this)">
<p><label for="name" id="namel">Your name: *</label>
<input class="text" type="text" name="name" /></p>
<p><label for="email" id="emaill">Your email: *</label>
<input class="text" type="text" name="email" /></p>
<p><label for="tel" id="tell">Your phone number:</label>
<input class="text" type="text" name="tel" /></p>
<p><label for="ref" id="refl">Your reference:</label>
<input class="text" type="text" name="ref" /></p>
<p><label for="enq" id="enql">Your enquiry: *</label>
<textarea name="enq"></textarea></p>
<p> </p>
<p><input type="submit" value="Send Email" /></p>
</form>
</div>
</body>
</html>
Any ideas why and how to fix? I've been trying for a while to get this to work.
Thanks,
Flic
I have a page with a form on it. I also have as part of the page some form validation using javascript. However, and this is rather frustrating, the form is being sent even when the validation is failing. If the form fails validation it shouldn't be sent.
The code is:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="geco.css" />
<link rel="stylesheet" type="text/css" href="geco_form.css" />
<SCRIPT type="text/javascript" language="Javascript">
function check(selected) {
var formok = false;
var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
if ((selected.name.value == "") || (selected.email.value == "") || (selected.enq.value == "")) {
formok = false;
if (selected.name.value == "") {
document.getElementById('namel').style.color = "#FF0000";
} if (selected.email.value == "") {
document.getElementById('emaill').style.color = "#FF0000";
} if (selected.enq.value == "") {
document.getElementById('enql').style.color = "#FF0000";
}
} else if (selected.email.value.match(emailExp)){
formok = false;
} else {
formok = true;
}
return formok;
}
</SCRIPT>
</head>
<body>
<div id="header"> </div>
<div id="links_bar">
<div class="links" id="nonclick"> </div>
<div class="links" id="click"><a href="index.html">About</a></div>
<div class="links" id="click"><a href="contact.html">Contact</a></div>
<div class="links" id="click"><a href="where.html">Where we are</a></div>
<div class="links" id="nonclick"> </div>
</div>
<div id="seperator"> </div>
<div id="content">
<h1>Email Us</h1>
<form name="contactform" method=post action="send.php" onsubmit="return check(this)">
<p><label for="name" id="namel">Your name: *</label>
<input class="text" type="text" name="name" /></p>
<p><label for="email" id="emaill">Your email: *</label>
<input class="text" type="text" name="email" /></p>
<p><label for="tel" id="tell">Your phone number:</label>
<input class="text" type="text" name="tel" /></p>
<p><label for="ref" id="refl">Your reference:</label>
<input class="text" type="text" name="ref" /></p>
<p><label for="enq" id="enql">Your enquiry: *</label>
<textarea name="enq"></textarea></p>
<p> </p>
<p><input type="submit" value="Send Email" /></p>
</form>
</div>
</body>
</html>
Any ideas why and how to fix? I've been trying for a while to get this to work.
Thanks,
Flic