ValidityState Flags Name Failed Rules
Each input.validity flag mirrors one HTML5 rule — typeMismatch, valueMissing, rangeUnderflow — so messages can target the exact failed check.
Code
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
JSON to Zod Schema ConverterInfer typed Zod z.object schemas from JSON payloads locally, supporting nested objects, merged array shapes, enums, nullable fields, and JSDoc comments.JSON Schema ValidatorValidate JSON data against a JSON Schema definition and get detailed error reports. Supports type checking, required fields, and common constraint validation.JSONL / NDJSON InspectorValidate NDJSON and JSON Lines line-by-line, pinpoint syntax errors by line number, analyze key frequencies and types, and export a pretty JSON array.CSS Color Name LookupTranslate between CSS keyword color names and hex values with RGB and HSL conversions, exact name matching, and nearest-color suggestions.
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.
Report a problem with this exampleOpens a pre-filled issue on GitHub.