The constraint validation API is a set of methods and properties
available on the following form elements:
Some also work on the <form> element.
example:
// return true element too long
button.validity.tooLong
Can be used with:
Set a custom error message
const email = document.getElementById("mail");
email.addEventListener("input", (event) => {
if (email.validity.typeMismatch) {
email.setCustomValidity("I am expecting an email address!");
} else {
email.setCustomValidity("");
}
});