How to stop form submission using JavaScript?

JavascriptObject Oriented ProgrammingFront End Technology

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.

raja
Published on 27-Feb-2018 15:28:31
Advertisements