Skip to content

JavaScript form onsubmit return false

  • by

JavaScript form onsubmit return false means do nothing on submit. In short, return false will cancel the event (or in this case, cancels the form submission).

<form name="foo" onsubmit="return false">

JavaScript form onsubmit return false

Simple example code validation returns false – your form will not submit and will not redirect too and When it returns true – your form will submit and will redirect (actually it’s a part of submit) id you have mentioned in action.

<!DOCTYPE html>
<html>
<body>
  <script type="text/javascript">
   function myFunctionName() {
    if (document.myForm.myText.value == ''){
      console.log(false);
      return false; 
    }  
    else{
      console.log(true);
    }
    return true;
  }
</script>

<form name="myForm" onSubmit="return myFunctionName()">
  <input type="text" name="myText">
  <input type="submit" value="Click Me">
</form>  

</body>
</html>

Output:

JavaScript form onsubmit return false

If you are using a button instead of submit as in my case below:

 <form name="myForm" onSubmit="myFunctionName(); return false">
    <input type="text" name="myText">
    <input type="button" value="Click Me" onclick="myFunctionName()">
 </from>

Do comment if you have any doubts or suggestions on this Js submit topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading