HTMLInputElement: invalid event - Web APIs | MDN

Syntax

Use the event name in methods like addEventListener(), or set an event handler property.

js

addEventListener("invalid", (event) => { })

oninvalid = (event) => { }

Event type

A generic Event.

Examples

If a form is submitted with an invalid value, the submittable elements are checked and, if an error is found, the invalid event will fire on the invalid element. In this example, when an invalid event fires because of an invalid value in the input, the invalid value is logged.

HTML

html

<form action="#">
  <div>
    <label>
      Enter an integer between 1 and 10:
      <input type="number" min="1" max="10" required />
    </label>
  </div>
  <div><input type="submit" value="submit" /></div>
</form>
<hr />
Invalid values:
<ul id="log"></ul>

JavaScript

js

const input = document.querySelector("input");
const log = document.getElementById("log");

input.addEventListener("invalid", (e) => {
  log.appendChild(document.createElement("li")).textContent = JSON.stringify(
    e.target.value,
  );
});

Result

Specifications

Specification
HTML
# event-invalid
HTML
# handler-oninvalid

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on by MDN contributors.