HTML Concepts: Constraint Validation
Published on July 27, 2021 (↻ December 17, 2021), filed under Development (RSS feed for all categories).
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 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 frontend engineering leader and tech author/publisher. I’ve worked as a technical lead for companies like Google and as an engineering manager for companies like Miro, I’m a contributor to several web standards, 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. (Be critical, interpret charitably, and give feedback.)
Read More
Maybe of interest to you, too:
- Next: One Favicon to Rule Them All
- Previous: CSS: “:has()” and the Lost Paradigm
- More under Development
- More from 2021
- Most popular posts
Looking for a way to comment? Comments have been disabled, unfortunately.
Get a good look at web development? Try WebGlossary.info—and The Web Development Glossary 3K. With explanations and definitions for thousands of terms of web development, web design, and related fields, building on Wikipedia as well as MDN Web Docs. Available at Apple Books, Kobo, Google Play Books, and Leanpub.