Character Count
Character count helps users know how much text they can enter when there is a limit on the number of characters.
Text Input with Character Count
Component Preview
Component Code
<form class="usa-form"> <div class="usa-character-count"> <div class="usa-form-group"> <label class="usa-label" for="with-hint-input">Text input</label> <span id="with-hint-input-hint" class="usa-hint"> This is an input with a character counter. </span> <input class="usa-input usa-character-count__field" id="with-hint-input" maxlength="25" name="with-hint-input" aria-describedby="with-hint-input-info with-hint-input-hint" /> </div> <span id="with-hint-input-info" class="usa-character-count__message"> You can enter up to 25 characters </span> </div> </form>
Textarea with Character Count
Component Preview
Component Code
<form class="usa-form"> <div class="usa-character-count"> <div class="usa-form-group"> <label class="usa-label" for="with-hint-textarea">Textarea</label> <span id="with-hint-textarea-hint" class="usa-hint"> This is a textarea with a character counter. </span> <textarea class="usa-textarea usa-character-count__field" id="with-hint-textarea" maxlength="50" name="with-hint-textarea" rows="5" aria-describedby="with-hint-textarea-info with-hint-textarea-hint" ></textarea> </div> <span id="with-hint-textarea-info" class="usa-character-count__message"> You can enter up to 50 characters </span> </div> </form>
Guidance
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.
- 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.
Accessibility guidance
- 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.
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 classusa-character-count__fieldand a message element (span or div) with the classusa-character-count__message. - Add a maxlength attribute to the input element.
- This will be used as the limit referenced in the message and for validation.
- Provide a backup message for environments without JavaScript.
- In your markup, add a default message in the
usa-character-count__messageelement that refers to the character limit. This will appear in instances when JavaScript does not load.
USWDS Documentation
For more guidance, view the USWDS Character Count Component Documentation.
Settings
This component has no settings.
Variants
This component has no variants.