Reputation: 82
I have this javascript code:
<script type="text/javascript">
<!--
function testCode () {
if (checkCode (document.getElementById('Code').value,document.getElementById('Type').value)) {
return true;
}
else {alert (Errors[ErrorNo])};
}
//-->
</script>
And my form:
<form accept-charset="UTF-8" action="verify.php" onsubmit="return testCode()" method="post">
// My stuff
<select id="Type" name="Type">
<option value="basic">Basic</option>
<option value="silver">Silver</option>
<option value="gold">Gold</option>
<option value="platinum">Platinum</option>
</select>
<input name="Code" type="text" id="Code">
<input type="submit" value="Submit">
</form>
After I submit the form, for example if I do not enter the code, will alert me with the error, but still submit the form, and I want to not submit it if the form is not validated.
Upvotes: 0
Views: 470
Reputation: 9681
You need to return false;
else {alert (Errors[ErrorNo]); return false;};
This will prevent execution of the rest of the events.
Upvotes: 1
Reputation: 3417
use return false;
if validation fails...
function testCode () {
if (checkCode (document.getElementById('Code').value,document.getElementById('Type').value)) {
return true;
}
else {
alert (Errors[ErrorNo]);
return false;
}
}
Upvotes: 2