0Pricing
JavaScript Academy · Lesson

The Constraint Validation API

Use built-in validity checking.

The Constraint Validation API is a free JavaScript Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Built-in Validation

HTML form fields support attributes like required, min, max, pattern, and type="email". The Constraint Validation API lets JavaScript inspect and control this built-in validation.

checkValidity

checkValidity returns a boolean and fires an invalid event on each failing control without showing the browser’s bubble.

const email = document.querySelector("#email");
console.log(email.checkValidity());

reportValidity

reportValidity is like checkValidity but also displays the browser’s validation message UI for invalid fields.

if (!form.reportValidity()) {
  console.log("show built-in errors");
}

The validity Object

Each control has a validity object of boolean flags describing exactly what is wrong.

console.log(email.validity.valueMissing);  // empty required
console.log(email.validity.typeMismatch);  // wrong format
console.log(email.validity.tooShort);

validity.valid

validity.valid is true only when the field passes every constraint, summarizing the other flags.

if (email.validity.valid) {
  console.log("ok");
}

validationMessage

validationMessage holds the localized message the browser would show for the current error.

console.log(email.validationMessage);

setCustomValidity

setCustomValidity marks a field invalid with your own message. A non-empty string means invalid; an empty string clears it.

function checkMatch() {
  if (pass.value !== confirm.value) {
    confirm.setCustomValidity("Passwords must match");
  } else {
    confirm.setCustomValidity("");
  }
}

Always Clear Custom Errors

If you set a custom message, you must clear it with an empty string once valid, or the field stays invalid forever.

confirm.addEventListener("input", () => {
  confirm.setCustomValidity("");
  checkMatch();
});

The invalid Event

Listen for the invalid event to run custom logic, like styling a field, when validation fails.

email.addEventListener("invalid", () => {
  email.classList.add("error");
});

Disabling Native Bubbles

Add novalidate to the form to suppress native popups while still using the API for your own messaging.

form.setAttribute("novalidate", "");
form.addEventListener("submit", (e) => {
  if (!form.checkValidity()) e.preventDefault();
});

Styling with :valid and :invalid

CSS pseudo-classes :valid and :invalid style fields based on their validity state automatically.

Quick Check

Custom validation rule.

Recap

The Constraint Validation API works with HTML constraint attributes. Use checkValidity/reportValidity to test (and optionally show) errors, inspect the validity flags and validationMessage, and add custom rules with setCustomValidity (clear with an empty string). The invalid event and :valid/:invalid CSS aid styling.

Frequently asked questions

Is the “The Constraint Validation API” lesson free?

Yes — the full text of “The Constraint Validation API” is free to read here on the web, and the JavaScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “The Constraint Validation API”?

Use built-in validity checking. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start JavaScript Academy?

No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “The Constraint Validation API” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this JavaScript Academy lesson?

Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Reading Input Values
  2. The submit Event
  3. Client-Side Validation
  4. The Constraint Validation API
← Back to JavaScript Academy