Lesson 8 of 8

Character counter

Goal

Show the limit before the user hits it

01

Explanation

A field with a maximum length should show that limit before the user reaches it, instead of suddenly refusing to take what they type with no explanation.

The maxlength attribute makes the browser itself enforce the limit, even on paste, so you don't need code to cut off the extra text. You calculate the counter on every input event with maxLength - value.length.

For people who can't see the counter, add aria-live="polite". It makes the screen reader announce the change after it finishes what it's saying, not in the middle of it the way assertive does. This live region has to be on the page from the start, because if it's added together with its text at the same moment, it most likely won't be announced.

02

Your task

Set the maximum length of #bio to 140 characters, write the number of characters left in #count on load and with every keystroke, make the counter a polite live region, then link the field to it with aria-describedby.

Checks

5 checks
  1. The browser enforces a 140-character limit
  2. The counter shows before any typing
  3. Typing lowers the counter
  4. The counter is a polite live region
  5. The field is linked to the counter
03

Hints

0/3
Track lessons0 of 8 done

In this track you'll write your own validation rules, tie one field to another, and write error messages a screen reader can announce.

Intermediate level, 8 lessons, 1.5 hours in total.

index.html
Preview
index.html100%
–/5

Press “Run and check” to grade your code.

5 checks listed under “Your task”.

Track lessons0 of 8 done

In this track you'll write your own validation rules, tie one field to another, and write error messages a screen reader can announce.

Intermediate level, 8 lessons, 1.5 hours in total.