PDA

View Full Version : JS False validation still executing code



korpela2
July 24th, 2008, 08:27 AM
I have javascript validating the following form. When the email is not entered an alert box is thrown up stating "not a valid email". however the action "collectMailPHP.php" is still executed. Can anyone help?

Here is the javascript:

<script type="text/javascript">

function validate_email(field,alerttxt)
{
with (field)
{
apos=value.indexOf("@");
dotpos=value.lastIndexOf(".");
if (apos<1||dotpos-apos<2)
{alert(alerttxt);return false;}
else {return true;}
}
}

function validate_form(thisform)
{
with (thisform)
{
if (validate_email(email_client,"Not a valid e-mail address!")==false)
{email.focus();return false;}

}
}
</script>

Here is the form;


<form action="collectMailPHP.php" method="post" onsubmit="return validate_form(this);">

<fieldset>

<legend> <span class="style1">Find Out More</span> </legend>
<ul>


<li>

<input type="text" class="contact_select" onfocus="if (this.value=='-Enter Email Address-'){this.value='';}" onblur="if (this.value==''){this.value='-Enter Email Address-';}" border="2" name="email_client" tabindex="1" value="-Enter Email Address-"></li>

<li>

<select class="contact_select" name="subject">

<option>I'm interested in...</option>
<option>Bingo Machines</option>
<option>Raffle Equipment</option>
<option>Arcade Games</option>
<option>Electronic Promotions</option>
<option>Other products</option>



</select>
</li>


<div id="submit_2">
<input type="submit" border="1" value="Submit" tabindex="5" />
</div>

</ul>
</fieldset>

</form>

actionAction
July 25th, 2008, 09:18 PM
your validate_form function does not have a return value, therefore the form finishes submission. Check the return value of validate_email in your validate_form function and return true or false based on whether or not validate_email returned true or false.