Web/API/HTMLInputElement/invalid event

From Get docs

The invalid event fires when a submittable element has been checked for validity and doesn't satisfy its constraints.

Bubbles No
Cancelable Yes
Interface Event
Event handler property GlobalEventHandlers.oninvalid

This event can be useful for displaying a summary of the problems with a form on submission. When a form is submitted, invalid events are fired at each form control that is invalid. The validity of submittable elements is checked before submitting their owner <form>, or after the checkValidity() method of the element or its owner <form> is called.

It is not checked on blur.

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

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

JavaScript

const input = document.querySelector('input')
const log = document.getElementById('log')

input.addEventListener('invalid', logValue)

function logValue(e) {
  log.textContent += e.target.value
}

Result

Specifications

Specification Status Comment
HTML Living StandardThe definition of 'Invalid event' in that specification. Living Standard
HTML 5.1The definition of 'Invalid event' in that specification. Recommendation
HTML5The definition of 'Invalid event' in that specification. Recommendation

Browser compatibility

Update compatibility data on GitHub

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
invalid event Chrome

Full support 10

Edge

Full support 12

Firefox

Full support 4

IE

Full support 10

Opera

Full support 10

Safari

Full support 5

WebView Android

Full support 4

Chrome Android

Full support 18

Firefox Android

Full support 64

Opera Android

Full support 12

Safari iOS

Full support 5

Samsung Internet Android

Full support 4.0

Legend

Full support  
Full support


See also