Compucles Posted April 23, 2011 Share Posted April 23, 2011 How do you get a form to not submit when it fails a validation check in a JavaScript function called from an OnClick event of the submit button? Link to comment Share on other sites More sharing options...
dsonesuk Posted April 23, 2011 Share Posted April 23, 2011 It is usually called from the 'onsubmit' of the form not 'onclick' of submit button as someone could submit by pressing the 'Enter' button, and so bypass the 'onclick' event.<form onsubmit="return valdatethis();" action="whateverpage.php" method="post">return will cause the function to run but wait for return value of 'true' to proceed, or 'false' to not proceed because data is not valid.function valdatethis(){var isvalid=true;//run code to validate codeif(....){isvalid=false;}if(....) { isvalid=false; }//if any fails the validation code in the if conditions above, the value for varible 'isvalid' will be set to false, else it will remain as true.//now all you have to do is return this varible valuereturn isvalid;}more detailed example shown here, slightly different but does the same thing.http://www.w3schools.com/js/js_form_validation.asp Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.