Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

ValidityState: overall validity and specific failures

Read `element.validity` to distinguish an overall valid state from a missing required value, a format mismatch, or a user-edited value that is too short.

Version: ECMAScript 2024 and the current web platform; runtime support varies

Syntax

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

element.validity returns a ValidityState object for a form control. Its Boolean properties are read-only and reflect the current state: a retained object is not a snapshot. This is a web platform API, not part of ECMAScript.

valid is true only when no failure flag is set, including flags outside this entry such as customError. More than one failure flag can be true at once. valueMissing is true when the value required by required is missing, as in an empty required email field.

typeMismatch is true when a nonempty value fails the HTML syntax rules for type="email" or type="url". For email, multiple allows a comma-separated list of addresses. An empty required email field has valueMissing, not typeMismatch. A syntax check does not establish that an address exists.

tooShort is true when minlength applies to an input or textarea, its dirty value flag is set, the user last changed the value, and the nonempty API value is shorter than the minimum in UTF-16 code units. minlength alone does not make a field required. Assigning .value does not trigger this failure, even if you then call checkValidity() or reportValidity().

Use valid to check the field's overall state and the failure flags to choose feedback. Validate submitted data again on the server: browser validation can be bypassed.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>ValidityState example</title>
</head>
<body>
  <label for="email">Email:</label>
  <input id="email" type="email" required minlength="8" autocomplete="off">
  <button type="button">Next</button>

  <script>
    (() => {
      const input = document.getElementById('email');
      const state = input.validity;
      const log = (step) => console.log([
        step, state.valid, state.valueMissing, state.typeMismatch, state.tooShort
      ].join(' '));

      input.value = '';
      log('empty');
      input.value = 'not-an-email';
      log('wrong-format');
      input.value = 'a@b.co';
      log('scripted-short');

      input.value = '';
      input.addEventListener('change', () => log('user-short'), { once: true });
    })();
  </script>
</body>
</html>

Result

empty false true false false
wrong-format false false true false
scripted-short true false false false
user-short false false false true

Open the example as an HTML page in a browser and view the output in the developer console. After the first three lines, type `a@b.co` into the field and press Tab. The columns after the step label are `valid`, `valueMissing`, `typeMismatch`, and `tooShort`, in that order. The same six-code-unit address is syntactically valid in both cases, but it violates `minLength = 8` only after the manual edit.

Related lessons

  • JavaScript · Lesson 22 · Intermediate · Version: 3

Sources