Character count helps users know how much text they can enter when there is a limit on the number of characters.
When to use the character count component
- Brevity is desired. When users are likely to provide more detail than is needed, and you want to force them to use fewer words. Note: this will likely increase the amount of time it takes users to submit the form because editing requires thinking. In the words of Mark Twain, “I didn’t have time to write a short letter, so I wrote a long one instead.”
- Legal requirement. When there is a legal reason where an entry must be under a certain number of characters.
When to consider something else
- Backend limitations. If your users keep hitting the character limit imposed by the backend of your service then try to increase the limit rather than use a character count.
- Already implied. If the character length is apparent or implied by the data type (i.e. phone number or zip code).
- Exceeding the character limit is highly unlikely. If the vast majority of users (well over 99%) are very unlikely to run afoul of backend validation, such as an address field that has a database field limit of 250 characters.
- Associate the character count message to the input. Use
aria-describedbyon the input to allow the message to be announced to those using screen readers.
- Use the
aria-liveattribute on character count message. Use
aria-live="polite"so that updates to character count message are also announced when using a screen reader.
Using the character count component
- Add component classes. The structure should include a base element with the class
usa-character-count. Inside of that base element there should be an input element (input or textarea) with the class
usa-character-count__fieldand an message element (span or div) with the class
- Add a
maxlengthattribute to the input element. This will be used as the limit referenced in the message and for validation.
Character count settings
This component has no settings.
Character count variants
This component has no variants.
Meaningful code and guidance updates are listed in the following table:
Improved screen reader experience. The character counter now includes a brief pause after input before announcing how many characters remain. Announcing the input no longer prevents the character counter from reading. More information: uswds#4976
Breaking Updated to Sass module syntax and new package structure. More information: uswds#4656