0Pricing
HTML Academy · Lesson

reportValidity and setCustomValidity

Trigger browser validation UI and set custom error messages.

Two APIs for Built-in UX

The Constraint Validation API exposes two methods that drive the browser's built-in validation UI: reportValidity (show the tooltip) and setCustomValidity (provide a custom error message). Together they let you customize messages while keeping the native presentation.

reportValidity vs checkValidity

checkValidity() silently returns true/false — no UI side effects. reportValidity() does the same check but additionally focuses the first invalid field and shows the browser's validation tooltip. Choose reportValidity when you want native UX for free.

document.forms.signup.addEventListener("submit", (e) => {
  if (!e.target.reportValidity()) {
    e.preventDefault();
    // browser already focused first invalid field and showed tooltip
  }
});

All lessons in this course

  1. The FormData API
  2. The Constraint Validation API
  3. reportValidity and setCustomValidity
  4. Form Reset Events and State Management
← Back to HTML Academy