Constraint validation
was introduced in HTML5.- Constraints like
required
,type=email
etc. helps browser in validation. - Browser blocks the form submission and informs user if validation fails.
- First we disable the default form validation so that we can add custom logic.
reportValidity()
: This method returnstrue
if the element's child controls satisfy their validation constraints. Whenfalse
is returned, cancelableinvalid
events are fired for each invalid child.- Then we customize the error messages.
validationMessage
: Property to get the validation message.setCustomValidity()
: Method to set the validation message.
const [input] = document.getElementsByTagName('input');
// Check message displayed
console.log(input.validationMessage);
// Override message
input.setCustomValidity('This is invalid');
// See updated message
console.log(input.validationMessage);
validity
: Property representing the valid state of the input.
{
"badInput": false,
"customError": false,
"patternMismatch": false, // pattern
"rangeOverflow": false, // max
"rangeUnderflow": false, // min
"stepMismatch": false, // step
"tooLong": false, // maxlength
"tooShort": false, // minlength
"typeMismatch": false, // type
"valid": true,
"valueMissing": false // required
}
- When browser validation happens, an invalid event if fired for each invalid input and trigger the error display.
- We can't check when an invalid input becomes valid again.
- To get around this we reset the error state before synchronizing the errors.
- Provides server validation.
- Provides a ValidityState like interface based on the validation attributes.
- Supports type inference and coercions
- 0 kb on the client.