Javascript Question

Soldato
Joined
22 Nov 2007
Posts
4,360
Hi guys got a small problem with form validation. The following code checks if the email field is empty, if it is gives a message if not I want it to submit the form. The problem is when the field is empty it will display the alert and then go on to submit the form which I don't want.

function validateForm(){
var email= document.getElementById('email').value;
if (email.length==''){
alert("please fill out email");}

else if (email.length>3)
{
document.forms['contact'].submit();
}

}
</script>

FORM CODE

<form name="contact" id='contact' method="post" action='ContactPHP.php'>
Your Email: <input name='email' id='email' type='text' /><br />
Your Email:Message:<br />
<textarea name='message' rows='15' cols='40'>
</textarea><br />
<input type="submit" value="submit" onClick="validateForm()"/>
</form>

Anyone see the problem?
 
Back
Top Bottom