The Validity State Object
You'll learn how to access detailed validation information for form inputs using the ValidityState object.
What is the Validity State?
When you add validation constraints to form inputs (like required, pattern, min, max), the browser doesn't just mark fields as valid or invalid — it tracks exactly which constraints are failing. The ValidityState object gives you this detailed breakdown.
Every form element has a .validity property that returns a ValidityState object with multiple boolean properties, each indicating whether a specific constraint has been violated.
The Core Properties
The ValidityState object includes properties like:
valid— true if all constraints passvalueMissing— true ifrequiredis violatedtypeMismatch— true if the input doesn't match itstype(like invalid email)patternMismatch— true ifpatternfailstooShort/tooLong— true ifminlength/maxlengthviolatedrangeUnderflow/rangeOverflow— true ifmin/maxviolatedstepMismatch— true ifstepviolated
Example Usage
const emailInput = document.querySelector('input[type="email"]');
emailInput.addEventListener('blur', () => {
const validity = emailInput.validity;
if (validity.valueMissing) {
console.log('Email is required');
} else if (validity.typeMismatch) {
console.log('Please enter a valid email address');
}
});
This lets you provide specific, helpful error messages instead of generic "this field is invalid" feedback.
Key Takeaway: The element.validity object gives you granular access to which validation constraints are failing, enabling precise user feedback for each type of validation error.