The Constraint Validation API
Use built-in validity checking.
The Constraint Validation API is a free JavaScript Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The JavaScript Academy course includes 4 lessons in total.
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.
Learn JavaScript with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 59
- Lessons
- 200
Frequently Asked Questions
Is the “The Constraint Validation API” lesson free?
Yes — any 3 lessons of the JavaScript Academy learning path, including “The Constraint Validation API”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The JavaScript Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.
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
- Reading Input Values
- The submit Event
- Client-Side Validation
- The Constraint Validation API