JavaScript Validation API: Complete Guide with Examples

Introduction to JavaScript Validation API

The JavaScript Validation API provides built-in browser validation for HTML forms without needing complex JavaScript. It helps validate user input before submission and provides user-friendly error messages.

Basic Methods

checkValidity() Method

Checks if an input element's value is valid according to its constraints.

setCustomValidity() Method

Sets a custom validation message for the input element.

1. Basic Validation Example

2. Constraint Validation DOM Properties

Property Description Type
validationMessage Returns the error message that would be displayed string
validity Returns a ValidityState object containing validation properties object
willValidate Indicates whether the element will be validated boolean
checkValidity() Returns true if element's value has no validity problems method
reportValidity() Reports invalid field to user (shows validation message) method
setCustomValidity(message) Sets a custom validation message method

3. ValidityState Properties

The validity property returns a ValidityState object with these boolean properties:

Property Description Triggered When
valueMissing Value is missing but required required attribute present but empty
typeMismatch Value doesn't match expected type Email/URL format incorrect
patternMismatch Value doesn't match pattern pattern regex doesn't match
tooLong Value exceeds maxLength Input longer than maxlength
tooShort Value is shorter than minLength Input shorter than minlength
rangeUnderflow Value is less than min Number below min attribute
rangeOverflow Value is greater than max Number above max attribute
stepMismatch Value doesn't fit step rules Doesn't match step increment
badInput Browser can't convert value Invalid numeric input
customError Custom validity set setCustomValidity() used
valid All validity constraints are satisfied All checks pass

4. Complete Validity Check Example

5. The rangeUnderflow Property Example

6. The rangeOverflow Property Example

7. Complete Range Validation Example (Both Underflow & Overflow)

8. Practical Application: Registration Form with Validation

Key Takeaways

  1. checkValidity() - Quick way to check if an input is valid
  2. setCustomValidity() - Set custom validation messages
  3. rangeUnderflow - Triggered when value is below minimum
  4. rangeOverflow - Triggered when value is above maximum
  5. Validation happens automatically with HTML5 constraints
  6. Customize messages for better user experience
  7. Use novalidate on form to disable browser default validation and use custom
  8. Real-time validation improves user experience

Best Practices

  1. Always provide clear error messages
  2. Use real-time validation for immediate feedback
  3. Combine HTML5 validation with JavaScript for complex rules
  4. Don't rely solely on client-side validation (always validate on server too)
  5. Use reportValidity() to show validation messages to users
  6. Style valid/invalid states differently for visual feedback

The JavaScript Validation API provides a powerful, built-in way to validate forms with minimal code, while still allowing for complex custom validation when needed.