Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

ValidityState — a form control's validation state

ValidityState exposes read-only error flags for a form control. It distinguishes a missing required value, an invalid format, and text that is too short, with an important exception for values assigned by scripts.

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

const state = input.validity;
state.valid;
state.valueMissing;
state.typeMismatch;
state.tooShort;

ValidityState is an HTML Web API interface, not part of ECMAScript. The object obtained through input.validity is live; its Boolean flags are read-only. ValidityState.valid is true when none of the other error flags is set.

ValidityState.valueMissing signals an unmet required constraint: for a text field, this means an empty value. ValidityState.typeMismatch means that a value violates the rules for the email or url type, not a JavaScript data type. An empty email address does not by itself cause this error.

ValidityState.tooShort applies to a nonempty value shorter than minlength in a control supporting that constraint. It requires a set dirty value flag and the last value change to have been a user edit. Assigning .value does not trigger this length check; minlength alone does not require a value.

The example retains one state object and reads it after changes. For 'x', tooShort remains false because a script assigns the value. Run it in a browser; having Node 24 in the repository does not demonstrate support for this API.

Examples

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

  const show = () => console.log(JSON.stringify({
    valid: state.valid,
    valueMissing: state.valueMissing,
    typeMismatch: state.typeMismatch,
    tooShort: state.tooShort
  }));

  show();
  input.value = 'x';
  show();
  input.value = 'a@example.com';
  show();
})();

Result

{"valid":false,"valueMissing":true,"typeMismatch":false,"tooShort":false}
{"valid":false,"valueMissing":false,"typeMismatch":true,"tooShort":false}
{"valid":true,"valueMissing":false,"typeMismatch":false,"tooShort":false}

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