Components

Character count

Help users enter text when there is a limit on the number of characters they can type.

<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, for example, 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, for example, your National Insurance number or credit card details."
  }
}) }}

You can configure this component using the Nunjucks macro arguments.

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, such as a submission has to 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 maximum character limit.

Sighted users will see a count message which updates as they type. Users of screen readers will hear the count message when they stop typing.

Users can continue typing after they’ve entered the maximum number of characters, but are told they’ve exceeded the limit. This lets users 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 above the textarea
  • if it’s below the bottom of the screen, users will still see it again when they scroll down to submit their response
  • it’s clearly separate from any error messages

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
  }
}) }}

You can configure this component using the Nunjucks macro arguments.

Consider if a word count is more helpful

In some cases, for example, if users have to enter a long answer, it may be more helpful to show a word count.

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"
  }
}) }}

You can configure this component using the Nunjucks macro arguments.

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, you need to 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?"
  }
}) }}

You can configure this component using the Nunjucks macro arguments.

Make the textarea slightly taller than the amount of content your limit allows. This lets users see and edit their full answer if they’ve only typed slightly over the limit.

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">
      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"
  }
}) }}

You can configure this component using the Nunjucks macro arguments.

If a user tries to submit their response with too many characters, you must show a validation error message above the field in addition to the count message below it.

The validation error message tells users what went wrong when they tried to submit their response and how to fix it. The count message provides inline 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

This component was originally developed and tested by the Government Digital Service (GDS) 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.

You can 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 has also been used in a number of real 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 described above.

In addition, 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.

Get in touch

If you’ve got a question, idea or suggestion share it in #govuk-design-system on cross-government Slack (open in app) or email the Design System team on govuk-design-system-support@digital.cabinet-office.gov.uk

Discuss ‘Character count’ on GitHub