HTML Concepts: Constraint Validation
Published on JulĀ 27, 2021 (updated DecĀ 17, 2021), filed under development, html (feed). (Share this on Mastodon orĀ Bluesky?)
In this installment of āHTML Concepts,ā letās look at HTML forms and constraint validation.
What is constraint validation?
When a form element (like input
) has attributes that define requirements for the elementās value (like being required
, or setting a minlength
, or following a pattern
), then these requirements (constraints) are being validated by the user agent.
Constraints are being validated as long as the element is not barred from it. This can happen when, for example, the element in question has a datalist
ancestor.
An element satisfies its constraints if it doesnāt suffer from any of the following validity states:
- the value is required but missing;
- the value suffers from a type mismatch (incorrect syntax);
- the value suffers from a pattern mismatch;
- the value is too long;
- the value is too short;
- the value suffers from an underflow (not the empty string and too low for
min
value); - the value suffers from an overflow (not the empty string and too high for
max
value); - the value suffers from step mismatch;
- the value has incomplete input;
- the value suffers from a custom error.
You run into constraint validation again in CSS. The validity pseudo-classes (:valid
and :invalid
), for example, match if constraints are (not) satisfied; and the range pseudo-classes (:out-of-range
and :in-range
) match if they (donāt) suffer from under- or overflow. Interestingly, the HTML specification may currently explain this better than Selectors Level 4.
Thatās it, at least for this brief conceptual description. The HTML specificationās Constraints section provides more detail. Love the specs.
About Me
Iām Jens (long: Jens Oliver Meiert), and Iām a web developer, manager, and author. Iāve been working as a technical lead and engineering manager for companies youāve never heard of and companies you use every day, Iām an occasional contributor to web standards (like HTML, CSS, WCAG), and I write and review books for OāReilly and Frontend Dogma.
I love trying things, not only in web development and engineering management, but also in other areas like philosophy. Here on meiert.com I share some of my experiences and views. (I value you being critical, interpreting charitably, and giving feedback.)