Entry 07

Form feedback with :user-invalid

Use it for inline form validation, in place of blur listeners and a "touched" class.

Open
The field has no color on load. Type three characters and leave it: the border takes the danger color. Eight or more and it takes the success color.
Code CSS · HTML · 2 blocks · 3 lines
input:user-invalid { border-color: var(--danger); }
input:user-valid { border-color: var(--success); }
<input type="password" required minlength="8">
Why it works, the rules and the credits 2 reasons · 2 rules · support · credits

Why it works

  1. :invalid matches an empty required field on page load. The :user- versions match only after the user has edited the field or tried to submit.
  2. Validity comes from HTML attributes: required, minlength, type, pattern.

Rules

  1. Color alone is not enough feedback. Pair it with text or an icon.
  2. The server still validates.

Support

Chrome
119
Firefox
88
Safari
16.5

Credits