HTML oninvalid Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

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.

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).

Attribute Values

The attribute value is always a script. Common patterns:

Value / patternDescriptionTry it
scriptAny JavaScript run when the control is invalidTry it
oninvalid="fn()"Call a named function on failed validationTry it
setCustomValidity(…)Custom message; clear with "" on inputTry it
checkValidity()Trigger a check (and invalid) without submittingTry it

Live Preview

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…

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>
Try It Yourself

How It Works

  1. Add a constraint — here required.
  2. User submits (or you call a validity API).
  3. Browser checks the control; if it fails, invalid fires.
  4. Read validationMessage for the browser’s default text, or set your own UI.

Example 2 — Custom Message with setCustomValidity

Replace the default bubble text; clear the custom error when the user types.

HTML
<form id="f">
  <label for="user">Username</label>
  <input id="user" name="user" required>
  <button type="submit">Submit</button>
</form>

<script>
  var user = document.getElementById("user");

  user.addEventListener("invalid", function () {
    user.setCustomValidity("Please enter a username.");
  });

  user.addEventListener("input", function () {
    user.setCustomValidity("");
  });

  document.getElementById("f").addEventListener("submit", function (e) {
    e.preventDefault();
  });
</script>
Try It Yourself

How It Works

  1. On invalid, call setCustomValidity("…").
  2. On input, call setCustomValidity("") so a fixed message does not stick forever.
  3. Browser shows your text in the native UI (unless you preventDefault() and draw your own).
  4. Prefer inline errors for accessible, styled forms in real apps.

Example 3 — Trigger invalid with checkValidity()

Validate without submitting the form.

HTML
<label for="age">Age (1–120)</label>
<input id="age" type="number" name="age" min="1" max="120" required>
<button type="button" id="check">Check</button>
<p id="log">Click Check with an empty or out-of-range value.</p>

<script>
  var age = document.getElementById("age");
  var log = document.getElementById("log");

  age.addEventListener("invalid", function () {
    log.textContent = "invalid — " + age.validationMessage;
  });

  document.getElementById("check").addEventListener("click", function () {
    if (age.checkValidity()) {
      log.textContent = "Valid.";
    }
  });
</script>
Try It Yourself

How It Works

  1. checkValidity() runs the same constraint checks as submit.
  2. If invalid, it fires invalid and returns false.
  3. reportValidity() is similar and also shows the browser’s UI.
  4. 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.

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 Chrome All modern
Full support
Mozilla Firefox All modern
Full support
Apple Safari All modern
Full support
Microsoft Edge All modern
Full support
Opera All modern
Full support
Samsung Internet Modern
Full support
Internet Explorer 10+
Supported
oninvalid Widely 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.

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.

Next: onkeydown

Run JavaScript when a key is pressed down.

onkeydown tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful