šŸš€ LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
šŸŽ“ COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
HTML MASTER CLASS /// LEARN TAGS /// BUILD STRUCTURE /// SEMANTIC WEB /// HTML MASTER CLASS /// LEARN TAGS ///

The HTML Constraint Validation API: Native Form Validation

Master the browser's built-in validity object, the silent checkValidity() method, and the UI-displaying reportValidity() method that together form the foundation of native HTML form validation.

⚔ Total XP: 0|šŸ’» html XP: 0

Skill Matrix

UNLOCK NODES BY LEARNING NEW TAGS.

Validation API

Native, built-in form validation.


šŸš€ LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
šŸŽ“ COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.

Long before JavaScript validation libraries, browsers already implement a complete, native Constraint Validation API. Understanding it first often eliminates the need for a library entirely, or at minimum informs a much simpler custom implementation.

1Every Field Has A Built-In validity Object

Every form control element — <input>, <select>, <textarea> — automatically exposes a read-only .validity property, a ValidityState object with named boolean flags reflecting exactly which HTML5 validation constraints (if any) the current value fails: valueMissing for an empty required field, typeMismatch for a malformed email or URL, patternMismatch for a value failing a pattern attribute, tooShort/tooLong for length constraints, and several others.

This means inspecting *why* a field is invalid requires zero custom logic — the browser has already computed it, continuously, based on the field's declared HTML constraints.

const input = document.querySelector('#email');
console.log(input.validity.valueMissing); // true if empty & required
console.log(input.validity.typeMismatch); // true if malformed email
localhost:3000
āœ“ Zero Custom Logic RequiredThe browser continuously computes and exposes exactly why a field is invalid.

2checkValidity(): Silent Constraint Testing

element.checkValidity() (available on individual fields and on the <form> element itself, checking all its fields at once) evaluates the current constraint state and returns a plain boolean — true if valid, false if not — firing an invalid event on any field that fails, but displaying zero visible UI on its own.

This silence is precisely what makes it useful as a building block: a developer can call checkValidity() to gate some custom logic (like disabling a submit button, or triggering a fully custom error-message rendering system) without the browser's own UI interfering or appearing unexpectedly.

if (!form.checkValidity()) {
  submitButton.disabled = true;
  // Custom error handling here — no native UI has appeared
}
localhost:3000
āœ“ Silent, Composable Building BlockcheckValidity() lets custom logic gate on validity state without any native UI side effect.

3reportValidity(): The Visible Counterpart

element.reportValidity() runs the identical underlying check as checkValidity(), but additionally displays the browser's native validation UI — the same small bubble pointing at an invalid field with a message like 'Please fill out this field' that automatically appears when a form is submitted with invalid data.

Calling it explicitly, rather than relying solely on automatic submission-time validation, lets a developer trigger native validation feedback at custom moments — for instance, re-validating and showing feedback for just one field the moment a user blurs out of it, rather than waiting for full form submission.

emailInput.addEventListener('blur', () => {
  emailInput.reportValidity(); // shows native bubble if invalid
});
localhost:3000
checkValidity() →
boolean only
reportValidity() →
boolean + visible native UI

4Step-by-Step Breakdown

Validation The Browser Already Knows How To Do. Long before reaching for a JavaScript validation library, browsers already ship a complete, native Constraint Validation API — every form control carries built-in validity state, and the browser can check and report it without a single line of custom validation logic.

Every Form Control Has A validity Object. Every form control element exposes a read-only .validity property — a ValidityState object with boolean flags like valueMissing, typeMismatch, and patternMismatch, letting JavaScript inspect exactly why a field is currently invalid without writing any validation logic itself.

The validity Object. What does checking input.validity.typeMismatch tell you for an <input type="email">?

  • →Whether the field is currently empty
  • →Whether the entered value doesn't match the expected email format
  • →Whether the field is currently disabled

checkValidity() Tests Without Showing UI. element.checkValidity() (or form.checkValidity() for the whole form) returns true/false based on current constraint state, firing an 'invalid' event on failure — but doesn't display any browser UI, giving JavaScript full control over what happens next.

checkValidity() Behavior. Does calling form.checkValidity() automatically display the browser's native validation error bubbles to the user?

  • →Yes, it always displays the native error UI immediately
  • →No, it silently returns true/false without showing any UI
  • →It varies unpredictably by browser with no consistent behavior

reportValidity() Shows The Browser's Native UI. element.reportValidity() performs the same check as checkValidity(), but additionally displays the browser's built-in validation message bubble pointing at the invalid field — the same UI that fires automatically on form submission, now triggerable on demand.

reportValidity() vs checkValidity(). What's the key difference between reportValidity() and checkValidity()?

  • →They're functionally identical with no real difference
  • →reportValidity() additionally displays the browser's native validation error UI
  • →checkValidity() is the one that shows UI, not reportValidity()

Validation API Understood. You now understand the browser's built-in Constraint Validation API: every field's validity object, the silent checkValidity() test, and the UI-displaying reportValidity() method — the foundation this entire Modern Forms module builds on before reaching for any JavaScript validation library.

Add A Visible Error Message Slot. The Constraint Validation API can show a custom message here when the required input is invalid.

Level Up šŸš€

Advanced cheat sheets, SEO tricks, and interview prep for this topic.

Browser Support

ChromeSupported

Fully supported.

FirefoxSupported

Fully supported.

SafariSupported

Fully supported.

EdgeSupported

Fully supported.

Accessibility (A11y)

1Native Validation UI Is Already Accessible By Default

The browser's built-in reportValidity() error bubbles are announced correctly to screen readers and receive focus automatically, a baseline of accessible behavior that hand-rolled custom validation UI must deliberately replicate.

SEO Implications

  • 1

    Native Validation Reduces JavaScript Payload Compared To A Full Validation Library

    Relying on the built-in Constraint Validation API for common cases avoids shipping an entire validation library's bundle weight, indirectly supporting faster page load and better Core Web Vitals scores.

Best Practices

Check What The Native Constraint Validation API Already Covers Before Reaching For A Validation Library

Many common validation needs (required fields, email format, length limits, numeric ranges) are already fully handled natively, at zero JavaScript bundle cost, before any custom code is written.

Use checkValidity() For Silent Logic Gating And reportValidity() When You Want Native Error UI To Appear

Choosing the right method for the situation avoids either accidentally suppressing helpful native feedback or triggering unwanted UI at the wrong moment.

Frequent Bugs

THE BUG

A form's submit handler runs custom logic even though a required field is empty.

THE FIX

Call form.checkValidity() (or reportValidity() for visible feedback) at the start of the submit handler and return early if it's false.

THE BUG

A team builds a fully custom JavaScript validation system without realizing basic checks were already available natively.

THE FIX

Audit which validations the native Constraint Validation API already covers, and reserve custom JS validation for genuinely custom business rules it can't express.

Real-World Examples

Field-Level Validation On Blur

Providing immediate native validation feedback as a user moves between form fields, rather than waiting for full submission.

form.querySelectorAll('input').forEach(input => {
  input.addEventListener('blur', () => input.reportValidity());
});

Interview Prep

?Frequently Asked Questions

Pascual Vila

Pascual Vila

Frontend Instructor // Code Syllabus

Common Pitfalls & Errors

The Error //

Building custom JS validation for cases the native API already handles

<!-- required, type=email, minlength, etc. are already validated natively -->

The Solution //

Check what the Constraint Validation API already covers before writing custom logic.

The Error //

Calling checkValidity() but expecting native error UI to appear

input.reportValidity(); // shows UI input.checkValidity(); // silent

The Solution //

Use reportValidity() instead if visible native feedback is desired.

Lesson Glossary

[01]Constraint Validation API

The browser's built-in native form validation system.

Code Preview
checkValidity(), reportValidity()

[02]ValidityState

An object exposing why a field currently fails validation.

Code Preview
input.validity

[03]checkValidity()

Silently tests validity, returns a boolean.

Code Preview
No UI shown

[04]reportValidity()

Tests validity and shows native error UI.

Code Preview
Shows error bubble

Continue Learning