HTML Input Validation is (maybe) Good

Built-in HTML input validation makes a good foundation for simple client-side validation. It just needs a little extra to make it accessible.

I think of client-side validation as a progressive enhancement for your users. You have to validate user input on the server (you can't trust what comes from the client), but some validation on the client makes for a nice UX. But that doesn't have to mean lots of JS code or using some validation library on your client. You can get pretty far with the browser's built-in HTML input validation. And then you can layer a little bit of JS on top of that to make it even better.

Let's take a look at a text input. You can use pattern, minlength and maxlength to provide constraints. You can use title to provide error message. You can conditionally style invalid input with :user-invalid.

Example

<label for="program_name">Name of program</label>
<input
  type="text"
  id="program_name"
  minlength="3"
  maxlength="20"
  pattern="[a-zA-Z0-9]+"
  title="Only alphabetical and numerical characters are accepted"
  required
/>
input:user-invalid {
  border: 4px solid red;
}

When the user attempts to submit the form the browser takes care of validating and showing the message.

A screenshot of Chrome.  The error message says 'Please match the requested format. Only alphabetical and numerical characters are accepted'
Example validation message on Chrome
A screenshot of Firefox.  The error message says 'Please match the requested format: Only alphabetical and numerical characters are accepted.'
Example validation message on Firefox
A screenshot of Safari.  The error message says 'Match the requested format: Only alphabetical and numerical characters are accepted'
Example validation message on Safari
A screenshot of Safari on iOS.  The error message says 'Match the requested format: Only alphabetical and numerical characters are accepted'
Example validation message on Safari on iOS

You can play with a live example at this CodePen.

BTW, I just noticed that Chrome and Firefox say "Please" but Safari doesn't 😄

Styling

You can't style the error popup, so if that's important to you then maybe you need to write your own error UI. A lot of times I don't mind leaning on browser UI when it's available (ie: most of the time I don't need my error messages in my website's overall brand/styling). Also, I think a lot of users have seen the browser's error UI before (from other sites that use native form validation), so there is some familiarity there for the user.

The Problem: Accessibility

Unfortunately, native form validation isn't very accessible.

I had assumed it was accessible, because most of the time when I lean on the browser to do something it handles accessibility much better than any userland code I would write. But after reading this excellent post from Adrian Roselli (an a11y expert), I learned my assumption was wrong.

Adrian's post lists some bugs opened against browsers to fix a11y issues. Some are marked as fixed, some are not. Here's hoping browser makers continue to improve the accessibility of native form validation.

Make it better with JS

I originally wrote this post to point out that native form validation is good enough (lean on the browser!) and that's all you need. But after reading about the accessibility issues, I think the right answer is using native form validation as the foundation, and then adding a bit of JS to make it more accessible.

How do we do that? This post by Cloud Four spells it out nicely, so I won't repeat it here. (If you mainly support evergreen browsers then I wouldn't worry too much about the first part "Removing invalid styles on page load for all browsers" since Chrome has shipped support for :user-invalid for a couple years now.)

Thanks to Manuel for reviewing this post and pointing out the accessibility issues with native form validation. I started out thinking the browser gives me all I need for client-side form validation, but learned that the browser provides a good start, but it's not enough by itself.

Hopefully you found this post helpful, if you have any questions or comments you can reach me via electronic mail.

PWA Install Pattern with QR Code and Token