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

The reportValidity() Method

Triggering validation and displaying browser-native error messages to users.

What You'll Learn

You'll learn how to trigger HTML5 validation and show the browser's built-in error messages to users using the reportValidity() method.


The Difference Between Checking and Reporting

You already know checkValidity() quietly checks if a form or input is valid, returning true or false without telling the user anything. Think of it as checking your homework silently in your head.

reportValidity() does the same validation check plus it alerts the user by displaying the browser's native validation messages—those little pop-up bubbles you've seen. It's like checking your homework and showing the teacher what's wrong.


How reportValidity() Works

When you call reportValidity() on a form or input element:

  1. It runs the same constraint validation as checkValidity()
  2. It returns true if valid, false if invalid
  3. If invalid, it displays the browser's default error message near the first invalid field
  4. It focuses that invalid field automatically

This makes it perfect for custom submit buttons or validation before form submission.


Using reportValidity()

const form = document.querySelector('#myForm');

// Check and show validation messages
if (!form.reportValidity()) {
  console.log('Form has errors - user sees them now');
}

Or on individual inputs:

const emailInput = document.querySelector('#email');

if (!emailInput.reportValidity()) {
  console.log('Email is invalid - error message shown');
}

When to Use It

Use reportValidity() when you want to validate before submission (like on a custom button click) or when you want to give users immediate feedback with the familiar browser validation UI.

Key Takeaway: reportValidity() validates your form or input and automatically displays browser-native error messages to the user, combining validation checking with user feedback in one method.