Validity Properties Explained
What you'll learn: How to interpret the specific boolean properties inside the Validity State object that tell you exactly why a form field is invalid.
Understanding the Validity Flags
Remember the Validity State object you just learned about? It contains eight specific boolean properties that act like diagnostic lights on your car's dashboard. Each one tells you a different reason why an input might be invalid.
The Eight Validity Properties
valueMissing - True when a required field is empty. Like forgetting to sign a contract.
typeMismatch - True when the value doesn't match the input's type (e.g., "hello" in an email field). Think of putting a square peg in a round hole.
patternMismatch - True when the value doesn't match the pattern attribute's regex. Like a password that doesn't meet complexity rules.
tooLong - True when the value exceeds maxlength (only possible programmatically, since browsers block typing past the limit).
tooShort - True when the value is shorter than minlength.
rangeUnderflow - True when a number is below the min attribute.
rangeOverflow - True when a number exceeds the max attribute.
stepMismatch - True when a number doesn't align with the step attribute (e.g., 2.5 when step="1").
Why This Matters
Instead of just knowing "something is wrong," these properties let you know what is wrong. You can check emailInput.validity.typeMismatch to display "Please enter a valid email" or ageInput.validity.rangeUnderflow to show "Must be at least 18."
Each property is either true (that error exists) or false (that specific validation passed).
Key Takeaway: The eight validity properties give you granular insight into exactly which validation constraint failed, enabling precise, helpful error messages for users.