ValidityState Flags Name Failed Rules

Each input.validity flag mirrors one HTML5 rule — typeMismatch, valueMissing, rangeUnderflow — so messages can target the exact failed check.

JavascriptForm Validation

Code

javascriptsnippet
19 lines
const form = document.createElement('form');
form.innerHTML = `
  <input name="email" type="email" required>
  <input name="age" type="number" min="18" max="99" value="12">
`;
document.body.append(form);

const email = form.elements.namedItem('email');
const age = form.elements.namedItem('age');

// input.validity is a ValidityState record: each flag names one failed rule
email.value = 'not-an-email';
console.log('email valid:', email.validity.valid, '| typeMismatch:', email.validity.typeMismatch);

email.value = '';
console.log('empty email valueMissing:', email.validity.valueMissing);

console.log('age rangeUnderflow:', age.validity.rangeUnderflow, '| valid:', age.validity.valid);
console.log('willValidate:', age.willValidate);

What the code does

Every operation runs against top-level bindings: form, email, age.

What to watch for

  • No package installation, no environment variables, no setup steps.
  • The snippet leans on document — browser-only surfaces a bare Node process cannot provide.

Applying it

Output lines are prefixed (email valid:, empty email valueMissing:, age rangeUnderflow:), making each step’s impact easy to trace.

Statements that decide the outcome

<input name="age" type="number" min="18" max="99" value="12">
`;
const email = form.elements.namedItem('email');
const form = document.createElement('form');

Interactive Tools for This Topic

FAQ

Does ValidityState Flags Name Failed Rules run in plain Node.js?
No — it touches `document`, which exist only in the browser. Run it in a page (or the DevTools console) instead of a bare Node process.