Hi —
I have a form with some javascript validation code for phone # and email address. If the user completes the form without making a mistake, the form submits normally. But if the user doesn’t initially include a valid email address (for example) and receives the error message, instead of waiting for the user to complete the form correctly, the form submits prematurely.
If anyone can spot what’s wrong, I’d very much appreciate any feedback.
Here is my code:
[CODE]
<script language=”javascript” type=”text/javascript”>
function validate_onclick()
{
var textbox3 = document.getElementById(“phone”);
var myregex = /d{3}-d{3}-d{4}/g;
if (textbox3.value == “”) {
alert(“You did not enter your phone number in the textbox. Please enter before submitting form. n”);
textbox3.focus();
return false;
}
else if (!textbox3.value.match(myregex)) {
alert(“Please enter a valid phone number.”);
textbox3.focus();
return false;
}
var textbox4 = document.getElementById(“email”);
var regex2 = /^w+-?w*@w+-?w*.(w+$)/gi;
if (textbox4.value == “”) {
alert(“You did not enter your email address in the textbox. Please enter before submitting form. n”);
textbox4.focus();
return false;
}
else if (!textbox4.value.match(regex2)) {
alert(“Please enter a valid email address.”);
textbox4.focus();
return false;
}
}
</script>
…
<label for=”phone”>Phone (###-###-####)</label><br>
<input type=”text” name=”phone” id=”phone” size=”50″ maxlength=”50″ tabindex=”3″>
<br /><br />
<label for=”email”>Email</label><br>
<input type=”text” name=”email” id=”email” size=”50″ maxlength=”50″ tabindex=”4″>
<br /><br />
…
<input type=”submit” name=”Form_Submit” value=”Submit Form” onClick=”validate_onclick();”>