I tried to cobble together some code from other sources before posting, and the code is close to working, but not quite there.
I have a contact form and I want to validate 3 of the 4 fields before it can be submitted, just to make sure they’re not empty.
Here is the form:
[CODE]<form name=”consult” action=”contact.php” method=”post” onsubmit=”return check()”>
<p><label>Name</label><input name=”Name” type=”text” id=”name” /></p>
<p><label>E-mail</label><input name=”E-mail” type=”text” id=”email” /></p>
<p><label>Phone</label><input name=”Phone” type=”text” id=”phone” /></p>
<p><label>Website</label><input name=”Company” type=”text” id=”website” /></p>
<p><button type=”submit” onclick=”check()”>Submit Request</button></p>
</form>
And here is the validation code I’m trying to use:
[CODE]<script language=”javascript”>
function check() {
if (document.consult.Name.value==””) {
alert(“You did not fill our your name.”);
return false;
}
else if (document.consult.E-mail.value==””) {
alert(“You did not fill our your e-mail.”);
return false;
}
else if (document.consult.Phone.value==””) {
alert(“You did not fill our your phone.”);
return false;
} else {
return true;
}
}
</script>
If I leave the form blank and click Submit, I get two alerts in a row that both say “You did not fill out your name”, and that’s it. If I enter something in the “Name” field, the form submits even if the other fields are blank.
I’m sure it’s something simple I’m overlooking, but I don’t know what. Would greatly appreciate any help..