Character count
Help users know how much text they can enter when there is a limit on the number of characters.
<div class="govuk-character-count" data-module="character-count" data-maxlength="200">
<div class="govuk-form-group">
<label class="govuk-label" for="with-hint">
Can you provide more detail?
</label>
<span id="with-hint-hint" class="govuk-hint">
Do not include personal or financial information like your National Insurance number or credit card details.
</span>
<textarea class="govuk-textarea js-character-count " id="with-hint" name="with-hint" rows="5" aria-describedby="with-hint-hint"></textarea>
</div>
<span id="with-hint-info" class="govuk-hint govuk-character-count__message" aria-live="polite">
You can enter up to 200 characters
</span>
</div>
{% from "character-count/macro.njk" import govukCharacterCount %}
{{ govukCharacterCount({
name: "with-hint",
id: "with-hint",
maxlength: 200,
label: {
text: "Can you provide more detail?"
},
hint: {
text: "Do not include personal or financial information like your National Insurance number or credit card details."
}
}) }}
When to use this component
Always test your service without a character count first.
Only use the character count component when there is a good reason for limiting the number of characters users can enter. For example, if there is:
- evidence that users are likely to enter more information than they need to
- a legal reason where an entry must be under a certain number of characters
When not to use this component
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.
How it works
It tells users how many characters they have remaining as they type into a textarea with a character limit.
Sighted users will see a count message that updates as they type. Screen reader users will hear the count message when they stop typing.
Users can enter more than the character limit, but are told they’ve entered too many characters. This lets them type or copy and paste their full answer, then edit it down.
The count message appears below the textarea so that:
- it’s clearly separate from any hint text or error message above the textarea
- if it’s below the bottom of the screen, users will still see it again when they scroll down to send their response
This component uses JavaScript. If JavaScript is not available, users will see a static message in place of the count message, telling them how many characters they can enter.
There are 2 ways to use the character count component. You can use HTML or, if you’re using Nunjucks or the GOV.UK Prototype Kit, you can use the Nunjucks macro.
<div class="govuk-character-count" data-module="character-count" data-maxlength="200">
<div class="govuk-form-group">
<h1 class="govuk-label-wrapper"><label class="govuk-label govuk-label--xl" for="label-as-page-heading">
Describe the nature of your event
</label>
</h1>
<textarea class="govuk-textarea js-character-count " id="label-as-page-heading" name="label-as-page-heading" rows="5"></textarea>
</div>
<span id="label-as-page-heading-info" class="govuk-hint govuk-character-count__message" aria-live="polite">
You can enter up to 200 characters
</span>
</div>
{% from "character-count/macro.njk" import govukCharacterCount %}
{{ govukCharacterCount({
id: "label-as-page-heading",
name: "label-as-page-heading",
maxlength: 200,
label: {
text: "Describe the nature of your event",
classes: "govuk-label--xl",
isPageHeading: true
}
}) }}
Consider if a word count is more helpful
In some cases it may be more helpful to show a word count. For example, if your question requires a longer answer.
Do this by setting data-maxwords
in the component markup. For example, data-maxwords="150"
will set a word limit of 150.
<div class="govuk-character-count" data-module="character-count" data-maxwords="150">
<div class="govuk-form-group">
<label class="govuk-label" for="word-count">
Enter a job description
</label>
<textarea class="govuk-textarea js-character-count " id="word-count" name="word-count" rows="5"></textarea>
</div>
<span id="word-count-info" class="govuk-hint govuk-character-count__message" aria-live="polite">
You can enter up to 150 words
</span>
</div>
{% from "character-count/macro.njk" import govukCharacterCount %}
{{ govukCharacterCount({
name: "word-count",
id: "word-count",
maxwords: 150,
label: {
text: "Enter a job description"
}
}) }}
Avoid narrow limits
When using a character count, try to set the limit higher than most users will need. Find out what this is by doing user research and data analysis.
If the limit is much higher than most users are likely to reach, you can choose to only display the message after a user has entered a certain amount.
To do this, set a threshold in the component markup. For example, data-threshold="75"
will show the count message only when users have entered 75% of the limit.
<div class="govuk-character-count" data-module="character-count" data-maxlength="112" data-threshold="75">
<div class="govuk-form-group">
<label class="govuk-label" for="word-count">
Can you provide more detail?
</label>
<textarea class="govuk-textarea js-character-count " id="word-count" name="word-count" rows="5">Type another letter into this field after this message to see the threshold feature</textarea>
</div>
<span id="word-count-info" class="govuk-hint govuk-character-count__message" aria-live="polite">
You can enter up to 112 characters
</span>
</div>
{% from "character-count/macro.njk" import govukCharacterCount %}
{{ govukCharacterCount({
name: "word-count",
id: "word-count",
maxlength: 112,
threshold: 75,
value: "Type another letter into this field after this message to see the threshold feature",
label: {
text: "Can you provide more detail?"
}
}) }}
Error messages
Error messages should be styled like this:
<div class="govuk-character-count" data-module="character-count" data-maxlength="350">
<div class="govuk-form-group govuk-form-group--error">
<label class="govuk-label" for="exceeding-characters">
Enter a job description
</label>
<span id="exceeding-characters-error" class="govuk-error-message">
<span class="govuk-visually-hidden">Error:</span> Job description must be 350 characters or less
</span>
<textarea class="govuk-textarea govuk-textarea--error js-character-count govuk-textarea--error" id="exceeding-characters" name="exceeding" rows="5" aria-describedby="exceeding-characters-error">A content designer works on the end-to-end journey of a service to help users complete their goal and government deliver a policy intent. Their work may involve the creation of, or change to, a transaction, product or single piece of content that stretches across digital and offline channels. They make sure appropriate content is shown to a user in the right place and in the best format.</textarea>
</div>
<span id="exceeding-characters-info" class="govuk-hint govuk-character-count__message" aria-live="polite">
You can enter up to 350 characters
</span>
</div>
{% from "character-count/macro.njk" import govukCharacterCount %}
{{ govukCharacterCount({
id: "exceeding-characters",
name: "exceeding",
maxlength: 350,
value: "A content designer works on the end-to-end journey of a service to help users complete their goal and government deliver a policy intent. Their work may involve the creation of, or change to, a transaction, product or single piece of content that stretches across digital and offline channels. They make sure appropriate content is shown to a user in the right place and in the best format.",
label: {
text: "Enter a job description"
},
errorMessage: {
text: "Job description must be 350 characters or less"
}
}) }}
If a user tries to send their response with too many characters, you must show an error message above the field as well as the count message below it.
The error message tells users what went wrong and how to fix it. The count message provides live feedback and updates as a user types.
Make sure errors follow GOV.UK guidance on writing error messages and have specific error messages for specific error states.
If the input is empty
Say ‘Enter [whatever it is]’.
For example, ‘Enter a summary’.
If the input is too long
Say ‘[whatever it is] must be [number] characters or less’.
For example, ‘Summary must be 400 characters or less’.
Research on this component
The Government Digital Service (GDS) developed and tested this component in a prototype of the ‘Apply for a temporary event notice’ service.
During this research, the component was tested with 17 users, including those with low digital skills and users with disabilities.
Read more about this research in the Design System wiki on GitHub.
Known issues and gaps
There is a known issue with JAWS where the field’s label is repeated every time the count message is played. This is repetitive and likely to be disruptive for users.
Services using this component
The component is used in a number of services, including the following.
Department for Education
Publish teacher training courses
Government Digital Service
Content publisher application
Next steps
Work is needed to fix the issue with JAWS.
More user research is needed to find out:
- how to decide between a character limit and a word limit
- if highlighting characters over the limit in red would be helpful for users
- how the component might work with lower as well as upper limits
- if enabling a character count on text inputs would be useful
If you’ve used this component, get in touch to share your user research findings.
Help improve this page
To help make sure the Character count page is useful, relevant and up to date, you can:
- share research or feedback about the Character count component on GitHub
- propose a change to this page - read more about how to propose changes in GitHub
Need help?
If you’ve got a question about the GOV.UK Design System you can contact the team: