JavaScript submit Event to Validate and Cancel Form Submission

Listen for the submit event on the form to check values before submission. Call event.preventDefault() during the handler if validation fails and the default submission should stop.

When a submit event fires

Clicking a submit button and submitting with Enter can reach the same handler. The browser checks built-in constraints such as required or pattern first. If validation fails, it does not fire submit. Whether Enter submits depends on the form structure. HTML Standard · Form submission

const form = document.querySelector('#code-form');

form.addEventListener('submit', event => {
  console.log(event.type); // 'submit'
});

Cancel submission when validation fails

The snippet works with a real form that has controls with the IDs shown. Run it after the form exists. Different values stop the submission and show a message. Matching values allow the default submission to continue.

const form = document.querySelector('#code-form');
const code = document.querySelector('#code');
const confirmation = document.querySelector('#confirmation');
const status = document.querySelector('#status');

form.addEventListener('submit', event => {
  if (code.value !== confirmation.value) {
    event.preventDefault();
    status.textContent = "The codes do not match.";
    confirmation.focus();
  }
});

Returning false from an addEventListener() handler does not cancel the submission. Validate again on the server in a real application. Browser validation gives users prompt feedback. DOM Standard · Event.preventDefault()

Use requestSubmit() for submission from JavaScript

Use form.requestSubmit(button) to take the path of activating a submit button from code. The browser validates first, then fires submit on success. event.submitter identifies the button. form.submit() skips constraint validation and this event. HTML Standard · requestSubmit()

const form = document.querySelector('#code-form');
const checkButton = document.querySelector('#check');
form.requestSubmit(checkButton);

Compare two codes in a complete form

Save the code as submit-validation.html in UTF-8 and open it in a browser. Enter two four-digit test codes. This file reports whether they match and cancels the default submission even when they do.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Compare codes before submission</title>
</head>
<body>
<form id="code-form">
  <label for="code">Test code (four digits)</label>
  <input id="code" inputmode="numeric" pattern="[0-9]{4}" required>
  <label for="confirmation">Enter the code again</label>
  <input id="confirmation" inputmode="numeric" pattern="[0-9]{4}" required>
  <button id="check" type="submit">Check input</button>
  <button id="request" type="button">Request submission from code</button>
</form>
<p id="status" role="status"></p>
<script>
const form = document.querySelector('#code-form');
const code = document.querySelector('#code');
const confirmation = document.querySelector('#confirmation');
const status = document.querySelector('#status');
const checkButton = document.querySelector('#check');

form.addEventListener('input', () => {
  status.textContent = '';
});

form.addEventListener('submit', event => {
  event.preventDefault();
  if (code.value !== confirmation.value) {
    status.textContent = "The codes do not match.";
    confirmation.focus();
    return;
  }
  status.textContent = "The codes match. This demo cancels submission.";
});

document.querySelector('#request').addEventListener('click', () => {
  form.requestSubmit(checkButton);
});
</script>
</body>
</html>

Enter 0427 and 0428 for the mismatch message. Enter 0427 twice for the match message. Leave a field blank or enter just three digits to see the browser’s validation prompt. The button that submits from code follows the same path.