Course contentsShow
← Web Development
Lesson 913 of 2,98216. Forms and ValidationFree lesson

Validity State Properties

Explore the ValidityState object and its properties to understand why an input is invalid.

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 — true if a required field is empty
  • typeMismatch — true if the value doesn't match the input type (e.g., invalid email)
  • patternMismatch — true if the value doesn't match the pattern attribute
  • tooLong / tooShort — true if the value exceeds maxlength or falls short of minlength
  • rangeUnderflow / rangeOverflow — true if the value is below min or above max
  • stepMismatch — true if the value doesn't align with the step attribute
  • badInput — true if the browser can't interpret the input (like letters in a number field)
  • customError — true if you've set a custom error with setCustomValidity()
  • valid — true only if all other properties are false

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.