Intermediate 15 min read

Validation and reportValidity()

By the end of this lesson, you'll be able to:

  • Use built-in validation attributes like required, pattern, min, and max
  • Call reportValidity() to trigger and display validation feedback
  • Use setCustomValidity() to enforce a custom validation rule

Prerequisites: "Checkbox Groups, Radio Groups, and Date Fields"

Built-in Validation Attributes

<lightning-input
    label="Email"
    type="email"
    required
    value={email}
    onchange={handleEmailChange}>
</lightning-input>

<lightning-input
    label="Age"
    type="number"
    min="18"
    max="120"
    value={age}
    onchange={handleAgeChange}>
</lightning-input>

required, pattern, min, max, minlength, and maxlength are recognized directly by base input components, with no custom validation code needed for these common cases.

reportValidity()

handleSubmit() {
    const isEmailValid = this.template.querySelector('[data-id="email"]').reportValidity();
    if (!isEmailValid) {
        return;
    }
    // proceed with submission
}

reportValidity() checks the field against its validation attributes and shows the browser/SLDS-styled error message inline if invalid, returning true or false. Calling it explicitly (commonly in a submit handler, before proceeding) is how validation is actually surfaced to the user — the attributes alone don't display anything until this is called.

setCustomValidity() for Custom Rules

handlePasswordConfirmChange(event) {
    const confirmInput = this.template.querySelector('[data-id="confirmPassword"]');
    if (event.target.value !== this.password) {
        confirmInput.setCustomValidity('Passwords do not match.');
    } else {
        confirmInput.setCustomValidity(''); // clears the custom error
    }
    confirmInput.reportValidity();
}

For rules the built-in attributes can't express — like comparing two fields against each other — setCustomValidity() sets a custom error message, and an empty string clears it. reportValidity() still needs to be called afterward to actually display the result.

Exercise

Write a lightning-input for a "Website" field requiring a value between 5 and 100 characters.

Show hint

Combine required with minlength/maxlength.

JAVASCRIPT

Exercise

Challenge: explain, as a comment, why setCustomValidity('') (an empty string) is necessary once a previously-invalid field becomes valid again.

Show hint

Think about what happens if the custom error message is never cleared.

JAVASCRIPT

Validation and reportValidity() Quiz

1. What does reportValidity() do?

2. What clears a custom validity message set via setCustomValidity()?

3. Validation attributes like required and pattern automatically display error messages without reportValidity() being called.

4. When would setCustomValidity() be needed over a built-in attribute?

5. What does reportValidity() return?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

Base input components carry built-in, HTML5-style validation — reportValidity() is the call that checks it and shows the user exactly what needs fixing.