I have a form with JavaScript validation. Upon there being an error, the submit button should 'grey-out' and the form should not be submitted. However, the last couple of functions seem to submit the form even though they pop the alert box. Why?
Button code:
<input type="submit" name="button" id="button"
onclick='return formvalidation();' value="Next" />
Non-working function example:
function BlankSite() {
var SiteNum= document.getElementsByName("sitesinput")[0].value;
if ((SiteNum == "") || (SiteNum == 0))
{
alert("You have not selected an amount of sites.")
document.forms[0].button.disabled = true;
return false;
}
}
Function initiator:
function formvalidation()
{
ZeroPhones();
BlankPC();
BlankSite();
BlankSeats();
phone_change();
} // End of formvalidation
This is very strange and I have tried various workarounds all to no avail!
Use the return value of the function to stop the execution of a form in JavaScript. False would return if the form fails to submit.
If return value is false, then set event's canceled flag. So, in short, return false will cancel the event (or in this case, cancels the form submission).
You use return false to prevent something from happening. So if you have a script running on submit then return false will prevent the submit from working.
You need to have return false;
in the function called by the onclick
, in this case formvalidation
.
Having some function called by the "root" function return false has no effect whatsoever. The return value is lost.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With