Components

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="govuk-character-count" data-maxlength="200">
  <div class="govuk-form-group">
    <label class="govuk-label" for="with-hint">
      Can you provide more detail?
    </label>
    <div id="with-hint-hint" class="govuk-hint">
      Do not include personal or financial information like your National Insurance number or credit card details.
    </div>
    <textarea class="govuk-textarea govuk-js-character-count" id="with-hint" name="with-hint" rows="5" aria-describedby="with-hint-info with-hint-hint"></textarea>
  </div>

  <div id="with-hint-info" class="govuk-hint govuk-character-count__message" aria-live="polite">
    You can enter up to 200 characters
  </div>

</div>
Nunjucks macro options

Use options to customise the appearance, content and behaviour of a component when using a macro, for example, changing the text.

Some options are required for the macro to work; these are marked as "Required" in the option description.

If you're using Nunjucks macros in production with "html" options, or ones ending with "html", you must sanitise the HTML to protect against cross-site scripting exploits.

Primary options
Name Type Description
id string Required. The id of the textarea.
name string Required. The name of the textarea, which is submitted with the form data.
rows string Optional number of textarea rows (default is 5 rows).
value string Optional initial value of the textarea.
maxlength string Required. If maxwords is set, this is not required. The maximum number of characters. If maxwords is provided, the maxlength argument will be ignored.
maxwords string Required. If maxlength is set, this is not required. The maximum number of words. If maxwords is provided, the maxlength argument will be ignored.
threshold string The percentage value of the limit at which point the count message is displayed. If this attribute is set, the count message will be hidden by default.
label object Required. Options for the label component. See label.
hint object Options for the hint component. See hint.
errorMessage object Options for the error message component. The error message component will not display if you use a falsy value for errorMessage, for example false or null. See errorMessage.
formGroup object Options for the form-group wrapper See formGroup.
classes string Classes to add to the textarea.
attributes object HTML attributes (for example data attributes) to add to the textarea.
spellcheck boolean Optional field to enable or disable the spellcheck attribute on the character count.
countMessage object Options for the count message See countMessage.
Options for formGroup
Name Type Description
classes string Classes to add to the form group (e.g. to show error state for the whole group)
Options for countMessage
Name Type Description
classes string Classes to add to the count message
Options for label
Name Type Description
text string Required. If `html` is set, this is not required. Text to use within the label. If `html` is provided, the `text` argument will be ignored.
html string Required. If `text` is set, this is not required. HTML to use within the label. If `html` is provided, the `text` argument will be ignored.
for string The value of the for attribute, the id of the input the label is associated with.
isPageHeading boolean Whether the label also acts as the heading for the page.
classes string Classes to add to the label tag.
attributes object HTML attributes (for example data attributes) to add to the label tag.
Options for hint
Name Type Description
text string Required. If `html` is set, this is not required. Text to use within the hint. If `html` is provided, the `text` argument will be ignored.
html string Required. If `text` is set, this is not required. HTML to use within the hint. If `html` is provided, the `text` argument will be ignored.
id string Optional id attribute to add to the hint span tag.
classes string Classes to add to the hint span tag.
attributes object HTML attributes (for example data attributes) to add to the hint span tag.
{% from "govuk/components/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="govuk-character-count" data-maxlength="200">
  <div class="govuk-form-group">
    <h1 class="govuk-label-wrapper"><label class="govuk-label govuk-label--l" for="label-as-page-heading">
        Describe the nature of your event
      </label>
    </h1>
    <textarea class="govuk-textarea govuk-js-character-count" id="label-as-page-heading" name="label-as-page-heading" rows="5" aria-describedby="label-as-page-heading-info"></textarea>
  </div>

  <div id="label-as-page-heading-info" class="govuk-hint govuk-character-count__message" aria-live="polite">
    You can enter up to 200 characters
  </div>

</div>
Nunjucks macro options

Use options to customise the appearance, content and behaviour of a component when using a macro, for example, changing the text.

Some options are required for the macro to work; these are marked as "Required" in the option description.

If you're using Nunjucks macros in production with "html" options, or ones ending with "html", you must sanitise the HTML to protect against cross-site scripting exploits.

Primary options
Name Type Description
id string Required. The id of the textarea.
name string Required. The name of the textarea, which is submitted with the form data.
rows string Optional number of textarea rows (default is 5 rows).
value string Optional initial value of the textarea.
maxlength string Required. If maxwords is set, this is not required. The maximum number of characters. If maxwords is provided, the maxlength argument will be ignored.
maxwords string Required. If maxlength is set, this is not required. The maximum number of words. If maxwords is provided, the maxlength argument will be ignored.
threshold string The percentage value of the limit at which point the count message is displayed. If this attribute is set, the count message will be hidden by default.
label object Required. Options for the label component. See label.
hint object Options for the hint component. See hint.
errorMessage object Options for the error message component. The error message component will not display if you use a falsy value for errorMessage, for example false or null. See errorMessage.
formGroup object Options for the form-group wrapper See formGroup.
classes string Classes to add to the textarea.
attributes object HTML attributes (for example data attributes) to add to the textarea.
spellcheck boolean Optional field to enable or disable the spellcheck attribute on the character count.
countMessage object Options for the count message See countMessage.
Options for formGroup
Name Type Description
classes string Classes to add to the form group (e.g. to show error state for the whole group)
Options for countMessage
Name Type Description
classes string Classes to add to the count message
Options for label
Name Type Description
text string Required. If `html` is set, this is not required. Text to use within the label. If `html` is provided, the `text` argument will be ignored.
html string Required. If `text` is set, this is not required. HTML to use within the label. If `html` is provided, the `text` argument will be ignored.
for string The value of the for attribute, the id of the input the label is associated with.
isPageHeading boolean Whether the label also acts as the heading for the page.
classes string Classes to add to the label tag.
attributes object HTML attributes (for example data attributes) to add to the label tag.
Options for hint
Name Type Description
text string Required. If `html` is set, this is not required. Text to use within the hint. If `html` is provided, the `text` argument will be ignored.
html string Required. If `text` is set, this is not required. HTML to use within the hint. If `html` is provided, the `text` argument will be ignored.
id string Optional id attribute to add to the hint span tag.
classes string Classes to add to the hint span tag.
attributes object HTML attributes (for example data attributes) to add to the hint span tag.
{% from "govuk/components/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--l",
    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="govuk-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 govuk-js-character-count" id="word-count" name="word-count" rows="5" aria-describedby="word-count-info"></textarea>
  </div>

  <div id="word-count-info" class="govuk-hint govuk-character-count__message" aria-live="polite">
    You can enter up to 150 words
  </div>

</div>
Nunjucks macro options

Use options to customise the appearance, content and behaviour of a component when using a macro, for example, changing the text.

Some options are required for the macro to work; these are marked as "Required" in the option description.

If you're using Nunjucks macros in production with "html" options, or ones ending with "html", you must sanitise the HTML to protect against cross-site scripting exploits.

Primary options
Name Type Description
id string Required. The id of the textarea.
name string Required. The name of the textarea, which is submitted with the form data.
rows string Optional number of textarea rows (default is 5 rows).
value string Optional initial value of the textarea.
maxlength string Required. If maxwords is set, this is not required. The maximum number of characters. If maxwords is provided, the maxlength argument will be ignored.
maxwords string Required. If maxlength is set, this is not required. The maximum number of words. If maxwords is provided, the maxlength argument will be ignored.
threshold string The percentage value of the limit at which point the count message is displayed. If this attribute is set, the count message will be hidden by default.
label object Required. Options for the label component. See label.
hint object Options for the hint component. See hint.
errorMessage object Options for the error message component. The error message component will not display if you use a falsy value for errorMessage, for example false or null. See errorMessage.
formGroup object Options for the form-group wrapper See formGroup.
classes string Classes to add to the textarea.
attributes object HTML attributes (for example data attributes) to add to the textarea.
spellcheck boolean Optional field to enable or disable the spellcheck attribute on the character count.
countMessage object Options for the count message See countMessage.
Options for formGroup
Name Type Description
classes string Classes to add to the form group (e.g. to show error state for the whole group)
Options for countMessage
Name Type Description
classes string Classes to add to the count message
Options for label
Name Type Description
text string Required. If `html` is set, this is not required. Text to use within the label. If `html` is provided, the `text` argument will be ignored.
html string Required. If `text` is set, this is not required. HTML to use within the label. If `html` is provided, the `text` argument will be ignored.
for string The value of the for attribute, the id of the input the label is associated with.
isPageHeading boolean Whether the label also acts as the heading for the page.
classes string Classes to add to the label tag.
attributes object HTML attributes (for example data attributes) to add to the label tag.
Options for hint
Name Type Description
text string Required. If `html` is set, this is not required. Text to use within the hint. If `html` is provided, the `text` argument will be ignored.
html string Required. If `text` is set, this is not required. HTML to use within the hint. If `html` is provided, the `text` argument will be ignored.
id string Optional id attribute to add to the hint span tag.
classes string Classes to add to the hint span tag.
attributes object HTML attributes (for example data attributes) to add to the hint span tag.
{% from "govuk/components/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="govuk-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 govuk-js-character-count" id="word-count" name="word-count" rows="5" aria-describedby="word-count-info">Type another letter into this field after this message to see the threshold feature</textarea>
  </div>

  <div id="word-count-info" class="govuk-hint govuk-character-count__message" aria-live="polite">
    You can enter up to 112 characters
  </div>

</div>
Nunjucks macro options

Use options to customise the appearance, content and behaviour of a component when using a macro, for example, changing the text.

Some options are required for the macro to work; these are marked as "Required" in the option description.

If you're using Nunjucks macros in production with "html" options, or ones ending with "html", you must sanitise the HTML to protect against cross-site scripting exploits.

Primary options
Name Type Description
id string Required. The id of the textarea.
name string Required. The name of the textarea, which is submitted with the form data.
rows string Optional number of textarea rows (default is 5 rows).
value string Optional initial value of the textarea.
maxlength string Required. If maxwords is set, this is not required. The maximum number of characters. If maxwords is provided, the maxlength argument will be ignored.
maxwords string Required. If maxlength is set, this is not required. The maximum number of words. If maxwords is provided, the maxlength argument will be ignored.
threshold string The percentage value of the limit at which point the count message is displayed. If this attribute is set, the count message will be hidden by default.
label object Required. Options for the label component. See label.
hint object Options for the hint component. See hint.
errorMessage object Options for the error message component. The error message component will not display if you use a falsy value for errorMessage, for example false or null. See errorMessage.
formGroup object Options for the form-group wrapper See formGroup.
classes string Classes to add to the textarea.
attributes object HTML attributes (for example data attributes) to add to the textarea.
spellcheck boolean Optional field to enable or disable the spellcheck attribute on the character count.
countMessage object Options for the count message See countMessage.
Options for formGroup
Name Type Description
classes string Classes to add to the form group (e.g. to show error state for the whole group)
Options for countMessage
Name Type Description
classes string Classes to add to the count message
Options for label
Name Type Description
text string Required. If `html` is set, this is not required. Text to use within the label. If `html` is provided, the `text` argument will be ignored.
html string Required. If `text` is set, this is not required. HTML to use within the label. If `html` is provided, the `text` argument will be ignored.
for string The value of the for attribute, the id of the input the label is associated with.
isPageHeading boolean Whether the label also acts as the heading for the page.
classes string Classes to add to the label tag.
attributes object HTML attributes (for example data attributes) to add to the label tag.
Options for hint
Name Type Description
text string Required. If `html` is set, this is not required. Text to use within the hint. If `html` is provided, the `text` argument will be ignored.
html string Required. If `text` is set, this is not required. HTML to use within the hint. If `html` is provided, the `text` argument will be ignored.
id string Optional id attribute to add to the hint span tag.
classes string Classes to add to the hint span tag.
attributes object HTML attributes (for example data attributes) to add to the hint span tag.
{% from "govuk/components/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="govuk-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 fewer
    </span>
    <textarea class="govuk-textarea govuk-textarea--error govuk-js-character-count govuk-textarea--error" id="exceeding-characters" name="exceeding" rows="5" aria-describedby="exceeding-characters-info 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>

  <div id="exceeding-characters-info" class="govuk-hint govuk-character-count__message" aria-live="polite">
    You can enter up to 350 characters
  </div>

</div>
Nunjucks macro options

Use options to customise the appearance, content and behaviour of a component when using a macro, for example, changing the text.

Some options are required for the macro to work; these are marked as "Required" in the option description.

If you're using Nunjucks macros in production with "html" options, or ones ending with "html", you must sanitise the HTML to protect against cross-site scripting exploits.

Primary options
Name Type Description
id string Required. The id of the textarea.
name string Required. The name of the textarea, which is submitted with the form data.
rows string Optional number of textarea rows (default is 5 rows).
value string Optional initial value of the textarea.
maxlength string Required. If maxwords is set, this is not required. The maximum number of characters. If maxwords is provided, the maxlength argument will be ignored.
maxwords string Required. If maxlength is set, this is not required. The maximum number of words. If maxwords is provided, the maxlength argument will be ignored.
threshold string The percentage value of the limit at which point the count message is displayed. If this attribute is set, the count message will be hidden by default.
label object Required. Options for the label component. See label.
hint object Options for the hint component. See hint.
errorMessage object Options for the error message component. The error message component will not display if you use a falsy value for errorMessage, for example false or null. See errorMessage.
formGroup object Options for the form-group wrapper See formGroup.
classes string Classes to add to the textarea.
attributes object HTML attributes (for example data attributes) to add to the textarea.
spellcheck boolean Optional field to enable or disable the spellcheck attribute on the character count.
countMessage object Options for the count message See countMessage.
Options for formGroup
Name Type Description
classes string Classes to add to the form group (e.g. to show error state for the whole group)
Options for countMessage
Name Type Description
classes string Classes to add to the count message
Options for label
Name Type Description
text string Required. If `html` is set, this is not required. Text to use within the label. If `html` is provided, the `text` argument will be ignored.
html string Required. If `text` is set, this is not required. HTML to use within the label. If `html` is provided, the `text` argument will be ignored.
for string The value of the for attribute, the id of the input the label is associated with.
isPageHeading boolean Whether the label also acts as the heading for the page.
classes string Classes to add to the label tag.
attributes object HTML attributes (for example data attributes) to add to the label tag.
Options for hint
Name Type Description
text string Required. If `html` is set, this is not required. Text to use within the hint. If `html` is provided, the `text` argument will be ignored.
html string Required. If `text` is set, this is not required. HTML to use within the hint. If `html` is provided, the `text` argument will be ignored.
id string Optional id attribute to add to the hint span tag.
classes string Classes to add to the hint span tag.
attributes object HTML attributes (for example data attributes) to add to the hint span tag.
{% from "govuk/components/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 fewer"
  }
}) }}

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 fewer’.
For example, ‘Summary must be 400 characters or fewer’.

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 that this page is useful, relevant and up to date, you can:

Need help?

If you’ve got a question about the GOV.UK Design System, contact the team.