Validity State Properties
You'll learn how to read the detailed reasons why a form input fails validation using the ValidityState object's individual properties.
What Is the ValidityState Object?
When you call input.validity, you get a ValidityState object. This object isn't just a single true/false value—it's a collection of specific properties that tell you exactly what validation rule was broken. Think of it like a medical report that doesn't just say "you're sick" but lists each specific symptom.
The Core Properties
The ValidityState object contains boolean properties for each validation constraint:
valueMissing—trueif arequiredfield is emptytypeMismatch—trueif the value doesn't match the input type (e.g., invalid email)patternMismatch—trueif the value doesn't match thepatternattributetooLong/tooShort—trueif the value exceedsmaxlengthor falls short ofminlengthrangeUnderflow/rangeOverflow—trueif the value is belowminor abovemaxstepMismatch—trueif the value doesn't align with thestepattributebadInput—trueif the browser can't interpret the input (like letters in a number field)customError—trueif you've set a custom error withsetCustomValidity()valid—trueonly if all other properties arefalse
Why This Matters
Instead of showing a generic "This field is invalid" message, you can check which property is true and display a specific, helpful message to the user.
Key Takeaway: The ValidityState object's properties let you pinpoint the exact validation rule that failed, enabling you to provide precise, user-friendly error messages.