HOWTO · JavaScript

How to Prevent Form Submission in JavaScript

Prevent a form from submitting when client-side validation fails with a submit event listener, an inline onsubmit handler, or jQuery.

On this page

To stop a form from submitting when its data fails client-side validation, handle the form’s submit event and call event.preventDefault() only when a check fails. Attach the listener to the form, not its submit button: the form’s submit event also covers keyboard submission. When the data is valid, leave the default action alone so the browser can submit the form.

Prevent a form submission with a submit event listener

A submit listener is the usual choice for new JavaScript code. This self-contained Node.js example models the event interface used by a browser form and shows a valid email reaching the normal submission path. In a page, register the same handler with form.addEventListener('submit', handleSubmit).

const form = {
  listener: null,
  addEventListener(type, callback) {
    if (type === 'submit') this.listener = callback;
  },
  submit(event) {
    this.listener(event);
    return !event.defaultPrevented;
  }
};

const email = 'reader@example.com';
const handleSubmit = (event) => {
  if (!email.includes('@')) event.preventDefault();
};

form.addEventListener('submit', handleSubmit);
const event = {
  defaultPrevented: false,
  preventDefault() {
    this.defaultPrevented = true;
  }
};
console.log(form.submit(event) ? 'Form may submit' : 'Submission prevented');

The example’s small form object lets the handler run in Node.js; in a browser, the browser supplies the form and event. In your page, read the relevant controls inside the handler and call event.preventDefault() when a check fails. Calling it cancels the browser’s default submission action; it does not navigate to another page or go back in history. A submit button click and pressing Enter in a form control both lead to the form’s submit event.

Use an inline onsubmit handler

An inline onsubmit handler is an alternative for existing markup. For example, <form onsubmit="return validateForm()"> calls a function when the form is submitted. Return false when validation fails to cancel submission; return true when the form is ready to submit. Keep the function available to the page and make sure it returns a value on every path. For new code, a listener usually keeps behavior separate from HTML.

Prevent submission with jQuery

If a project already uses jQuery, attach a handler to the form with $('#myForm').on('submit', function (event) { ... }). Check the form values inside the callback and call event.preventDefault() only when validation fails. Do not call $('#myForm').submit() from the handler to prevent submission: that triggers submission behavior rather than canceling the browser’s default action. jQuery is optional; native addEventListener provides the same form event without the dependency.

Validate inputs and provide feedback

When a check fails, prevent submission and show a concise message next to the field that needs attention. Associate that message with its control, expose the error to assistive technology, and move focus to the first field that needs correction when appropriate. Avoid relying on an alert as the only feedback. When all checks pass, allow submission to continue and let the server validate the received data too; client-side checks improve interaction but do not secure data by themselves.

Use HTML constraints such as required, type="email", and minlength when they fit the form. The browser can report these failures before a normal submit event is sent; custom checks can add rules that HTML constraints do not express. Do not disable the submit button as a substitute for validation, since users still need a clear path to correct their input.