Computer >> Computer tutorials >  >> Programming >> Javascript

How to stop form submission using JavaScript?


Use the return value of the function to stop the execution of a form in JavaScript.

<form onsubmit = "event.preventDefault(); myValidation();">

The JavaScript function would be like the following −

function myValidation()
{
   if(check if your conditions are not satisfying)
   {
      alert("Oops! Validation failed!");
      returnToPreviousPage();
      return false;
   }
   alert("Validations successful!");
   return true;
}

False would return if the form fails to submit.