Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Show Errors Only After Input

In this lesson, you gave the work-day sign-up form a red border on any field that’s wrong, in the site’s own style, and made it wait until the visitor has had a go before it shows.

Key Points

  • The form already refuses bad input, but the browser only says so when someone clicks Sign up, and then its bubble points at one field at a time. A style on the field itself can say more, right in the page.
  • :invalid is a pseudo-class, like the :hover and :focus-visible the nav links already use. It matches a field whose value fails its checks. The lesson added an --error token, a dark red, to the tokens layer, and an input:invalid rule at the end of the components layer.
  • Red already: on a fresh page, Your name and Email have red borders before Mei has touched a thing. An empty required field fails its check from the moment the page loads, and :invalid describes the value right now.
  • CSS matches states, not events. There’s no code that runs when someone gets a field wrong. A selector matches whatever state the field is in at this moment.
  • :user-invalid matches the same invalid values, but only once the visitor has changed the field and left it, or tried to submit. The browser keeps track of that as part of the field’s state, so CSS is still matching a state. Changing :invalid to :user-invalid makes the page load calm.
  • Tabbing through a field doesn’t count. Typing mei.lin into Email changes nothing until Mei presses Tab to leave it, and then the border turns red. Once a field counts as touched, the rule follows its value as she types, and it stops matching as soon as the address is valid. While the field has focus, Chrome’s focus ring is drawn over the border.
  • Trying to submit counts too: clicking Sign up on an empty form turns every empty required field red at once, and the browser’s bubble still points at the first one.
  • A red border is only a start. Some visitors can’t tell the color, and a border doesn’t say what’s wrong. MDN’s :user-invalid page has more on when it matches.

Final Code

In style.css, the tokens layer gains one custom property after --free:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Your sign-up form now refuses an empty name, a bad email, and more than eight hours. But Mei only finds out when she clicks Sign up, and then the browser’s bubble points at one field at a time.

00:13 The form can say more in the page itself. Give a field that’s wrong a red border, in the site’s own style.

00:22 The site already uses pseudo-classes, such as hover and focus-visible on the nav links. Each matches an element in a certain state. Forms add one for this, the :invalid pseudo-class, which matches a field whose value fails its checks.

00:37 First, add an error color to the tokens layer, a dark red. Then, at the end of the components layer, give every invalid input a two-pixel solid border in that color.

00:51 Mei opens the page. She hasn’t touched a thing. Your turn: pause the video and predict what the border of Your name looks like. It’s red already, and so is Email.

01:04 That was a detour, but a useful one. The :invalid pseudo-class doesn’t wait for Mei. It describes the value right now, and an empty required field fails its check from the moment the page loads.

01:16 If you pictured code that runs when someone gets it wrong, CSS doesn’t work that way. It matches states, not events. So the form scolds Mei before she starts.

01:29 The fix is one word. Change :invalid to :user-invalid, and save. It matches the same invalid values, but only once the visitor has changed the field and left it, or tried to submit.

01:42 The browser remembers whether that has happened, as part of the field’s state. So CSS is still matching a state here. The page now loads calm.

01:52 Mei presses Tab through Your name and Email without typing. Passing through doesn’t count, so both stay calm.

02:00 Now she types mei.lin into Email. While she’s typing, nothing changes. She presses Tab to leave the field, and the border turns red.

02:11 She goes back and adds @example.com. She has left the field once, so it counts as touched. Now the rule follows the value as she types, and it stops matching as soon as the address is valid.

02:23 While she’s in the field, Chrome’s focus ring covers the border, so the label under the window shows the match.

02:31 Some visitors don’t fill in anything. Ana opens the page and clicks Sign up straight away. Trying to submit counts as having a go. Email is red now. Your name is red too, under the focus ring, and the browser’s bubble points at it.

02:48 So the :invalid pseudo-class matches a value that fails its checks, right now. :user-invalid matches the same value, but only after the visitor has changed the field and left it, or tried to submit.

03:01 Use :user-invalid for your error styles.

03:05 A red border is a start toward an accessible error. Some visitors can’t tell the color, though, and a border doesn’t say what’s wrong. The next lesson, Style Form Controls to Match, adds a message next to the field, and makes the fields look like the rest of the site.

Become a Member to join the conversation.