The oninvalid attribute is an event-handler attribute. It runs JavaScript when the invalid event fires — a submittable control was checked for validity and does not satisfy its constraints (required, pattern, min/max, type="email", and similar).
Checks happen before the owner form submits, or when you call checkValidity() / reportValidity(). Per MDN, validity is not checked on blur. Related: novalidate, onsubmit, and oninput (clear custom errors while typing).
Baseline · Widely available.
Not deprecated and not experimental. The event is cancelable (you can preventDefault() to suppress the browser’s default message UI) and does not bubble.
Discouraged as inline HTML.
MDN discourages HTML event handler attributes. Prefer el.addEventListener("invalid", handler) in production; use inline oninvalid for quick demos and learning.
Syntax
HTML Syntax
syntax.html
<input|textarea|select oninvalid="script">
script is JavaScript that runs when the control fails a validity check. Equivalent JS: el.oninvalid = handler or el.addEventListener("invalid", handler).
Values
Attribute Values
The attribute value is always a script. Common patterns:
Leave the field empty and click Submit. Constraint validation fails, invalid fires, and the status updates. (Submit is canceled so the page stays here.)
Demo form
Uses required. Fill a value and submit again to see a valid path.
Preview
Waiting for invalid…
Code
HTML Examples
Short, copy-ready snippets. Use Try It Yourself for a live editor.
Example 1 — Message When Validation Fails
Show status text when a required field is empty on submit.
HTML
<form id="f">
<label for="name">Name</label>
<input id="name" name="name" required
oninvalid="onBad(event)">
<button type="submit">Submit</button>
</form>
<p id="status">Submit with an empty name.</p>
<script>
function onBad(event) {
document.getElementById("status").textContent =
"invalid — " + event.target.validationMessage;
}
document.getElementById("f").addEventListener("submit", function (e) {
e.preventDefault();
document.getElementById("status").textContent = "Form is valid.";
});
</script>
checkValidity() runs the same constraint checks as submit.
If invalid, it fires invalid and returns false.
reportValidity() is similar and also shows the browser’s UI.
Still not on blur — leaving the field alone does not fire invalid.
Edge Cases & Pitfalls
Most “invalid never fired” bugs are expecting it on blur or listening only on the form without capture.
blur
Not checked on blur
Per MDN, leaving a field does not run this check. Use submit, checkValidity(), or reportValidity().
bubble
Does not bubble
A bubbling listener on <form> will miss invalid. Listen on each control, or use capture: form.addEventListener("invalid", handler, true).
novalidate
novalidate skips submit checks
novalidate (or formnovalidate) turns off native validation on submit — so invalid will not fire from that submit. You can still call checkValidity() yourself.
custom
Clear setCustomValidity
A non-empty custom message keeps the field invalid until you call setCustomValidity("") (usually on input).
server
Client checks are not enough
Constraint validation is a UX aid. Always validate again on the server.
Compatibility
Browser Support
oninvalid is an HTML event-handler attribute for constraint validation in the browser. It is not a Node.js, Deno, or Bun API.
✓ Baseline · Widely available
oninvalid
Baseline widely available. Chrome, Firefox, Safari, Edge, and Opera support invalid with HTML5 constraint validation.
96%Global support (approx.)
Google ChromeAll modern
Full support
Mozilla FirefoxAll modern
Full support
Apple SafariAll modern
Full support
Microsoft EdgeAll modern
Full support
OperaAll modern
Full support
Samsung InternetModern
Full support
Internet Explorer10+
Supported
oninvalidWidely supported
Bottom line: Safe to use with HTML5 constraints. Prefer addEventListener('invalid') in apps, clear custom validity on input, and remember it is not a blur event.
Remember
Key Takeaways
Role: control failed a constraint check.
Triggers: submit check, checkValidity(), reportValidity() — not blur.
Does not bubble — listen per control or use capture.
Custom text:setCustomValidity, then clear on input.
One line:oninvalid means a constraint check failed — usually on submit, not when the field blurs.
Frequently Asked Questions
It is an event-handler attribute. When a submittable control is checked for validity and fails its constraints (required, pattern, min/max, type=email, and so on), the invalid event fires and the JavaScript in oninvalid runs.
When the browser checks validity and the control is invalid — most often on form submit, or when you call checkValidity() or reportValidity() in JavaScript. Per MDN, it is not checked on blur.
Submittable form controls that take part in constraint validation — commonly input, textarea, and select with constraints like required or pattern.
Call setCustomValidity('Your message') in the invalid handler (or before checking). Clear it with setCustomValidity('') on input so the field can become valid again.
No. Listen on each control, or use addEventListener on a parent with capture: true if you want one form-level handler.
No. The invalid event and oninvalid handler are standard and Baseline widely available. Inline HTML handlers are discouraged in production in favor of addEventListener.