How to prevent form from being submitted?

How to prevent form from being submitted?

I have a form that has a submit button in it somewhere.
However, I would like to somehow ‘catch’ the submit event and prevent it from occurring.
Is there some way I can do this?
I can’t modify the submit button, because it’s part of a custom control.

Solutions/Answers:

Solution 1:

Unlike the other answers, return false is only part of the answer. Consider the scenario in which a JS error occurs prior to the return statement…

html

<form onsubmit="return mySubmitFunction(event)">
  ...
</form>

script

function mySubmitFunction()
{
  someBug()
  return false;
}

returning false here won’t be executed and the form will be submitted either way. You should also call preventDefault to prevent the default form action for Ajax form submissions.

function mySubmitFunction(e) {
  e.preventDefault();
  someBug();
  return false;
}

In this case, even with the bug the form won’t submit!

Alternatively, a try...catch block could be used.

function mySubmit(e) { 
  e.preventDefault(); 
  try {
   someBug();
  } catch (e) {
   throw new Error(e.message);
  }
  return false;
}

Solution 2:

You can use inline event onsubmit like this

<form onsubmit="alert('stop submit'); return false;" >

Or

<script>
   function toSubmit(){
      alert('I will not submit');
      return false;
   }
</script>

<form onsubmit="return toSubmit();" >

Demo

Related:  JavaScript - Get Portion of URL Path

Now, this may be not a good idea when making big projects. You may need to use Event Listeners.

Please read more about Inline Events vs Event Listeners (addEventListener and IE’s attachEvent) here. For I can not explain it more than Chris Baker did.

Both are correct, but none of them are “best” per se, and there may be
a reason the developer chose to use both approaches.

Solution 3:

Attach an event listener to the form using .addEventListener() and then call the .preventDefault() method on event:

const element = document.querySelector('form');
element.addEventListener('submit', event => {
  event.preventDefault();
  // actual logic, e.g. validate the form
  console.log('Form submission cancelled.');
});
<form>
  <button type="submit">Submit</button>
</form>

I think it’s a better solution than defining a submit event handler inline with the onsubmit attribute because it separates webpage logic and structure. It’s much easier to maintain a project where logic is separated from HTML. See: Unobtrusive JavaScript.

Using the .onsubmit property of the form DOM object is not a good idea because it prevents you from attaching multiple submit callbacks to one element. See addEventListener vs onclick
.

Related:  convert iso date to milliseconds in javascript

Solution 4:

Try this one…

HTML Code

<form class="submit">
    <input type="text" name="text1"/>
    <input type="text" name="text2"/>
    <input type="submit" name="Submit" value="submit"/>
</form>

jQuery Code

$(function(){
    $('.submit').on('submit', function(event){
        event.preventDefault();
        alert("Form Submission stopped.");
    });
});

or

$(function(){
    $('.submit').on('submit', function(event){
       event.preventDefault();
       event.stopPropagation();
       alert("Form Submission prevented / stopped.");
    });
});

Solution 5:

The following works as of now (tested in chrome and firefox):

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

where validateMyForm() is a function that returns false if validation fails. The key point is to use the name event. We cannot use for e.g. e.preventDefault()

Solution 6:

var form = document.getElementById("idOfForm");
form.onsubmit = function() {
  return false;
}