Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

HTMLInputElement.validity — input validity state

HTMLInputElement.validity exposes an input's current constraint-validation state as a ValidityState object. Its flags identify particular problems, while valid indicates that no error flag is set.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

input.validity
input.validity.valid
input.validity.valueMissing

HTMLInputElement.validity is a read-only property returning a live ValidityState object: a saved reference reflects later changes to the input. Its flags are read-only too. This browser API is defined by HTML, not ECMAScript; the availability of Node.js alone does not establish support.

valueMissing indicates a missing required value, typeMismatch a type-format mismatch, and patternMismatch a mismatch with pattern. The other error flags are tooLong, tooShort, rangeUnderflow, rangeOverflow, stepMismatch, badInput, and customError. Their applicability depends on the input type and constraints. valid is true when all error flags are false.

Reading validity neither displays a message nor fires invalid. checkValidity() checks the control and can fire invalid; reportValidity() can additionally present the problem to the user. willValidate determines participation in validation: a disabled input can still have error flags even though it does not participate in checking.

setCustomValidity('Blocked') sets customError; clear it with setCustomValidity(''). Fixing the value alone does not clear it. The example demonstrates updates through the saved state object without reading input.validity again. Browser validation does not replace server-side data checks.

Examples

const input = document.createElement('input');
input.type = 'email';
input.required = true;
const state = input.validity;

console.log(state.valueMissing, state.valid);
input.value = 'learner@example.com';
console.log(state.valueMissing, state.valid);
input.setCustomValidity('Blocked');
console.log(state.customError, state.valid);
input.setCustomValidity('');
console.log(state.customError, state.valid);

Result

true false
false true
true false
false true

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources