Components

Character count

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

Do not include personal information like your service number.
You can enter up to 200 characters
<div
class="govuk-form-group govuk-character-count"
data-module="govuk-character-count"
data-maxlength="200"
>

<h1 class="govuk-label-wrapper">
<label class="govuk-label govuk-label--l" for="with-hint">
Can you provide more detail?
</label>
</h1>
<div id="with-hint-hint" class="govuk-hint">
Do not include personal information like your service number.
</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 id="with-hint-info" class="govuk-hint govuk-character-count__message">
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 option 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 option 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.

The label used by the character count component.

See label.

hint object

Can be used to add a hint to the character count component.

See hint.

errorMessage object

Can be used to add an error message to the character count component. The error message component will not display if you use a falsy value for errorMessage, for example false or null.

See error message.

formGroup object

Additional options for the form group containing the character count component.

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

Additional options for the count message used by the character count component.

See countMessage.

textareaDescriptionText string

Message made available to assistive technologies to describe that the component accepts only a limited amount of content. It is visible on the page when JavaScript is unavailable. The component will replace the %{count} placeholder with the value of the maxlength or maxwords parameter.

charactersUnderLimitText object

Message displayed when the number of characters is under the configured maximum, maxlength. This message is displayed visually and through assistive technologies. The component will replace the %{count} placeholder with the number of remaining characters. This is a pluralised list of messages.

charactersAtLimitText string

Message displayed when the number of characters reaches the configured maximum, maxlength. This message is displayed visually and through assistive technologies.

charactersOverLimitText object

Message displayed when the number of characters is over the configured maximum, maxlength. This message is displayed visually and through assistive technologies. The component will replace the %{count} placeholder with the number of characters above the maximum. This is a pluralised list of messages.

wordsUnderLimitText object

Message displayed when the number of words is under the configured maximum, maxwords. This message is displayed visually and through assistive technologies. The component will replace the %{count} placeholder with the number of remaining words. This is a pluralised list of messages.

wordsAtLimitText string

Message displayed when the number of words reaches the configured maximum, maxwords. This message is displayed visually and through assistive technologies.

wordsOverLimitText object

Message displayed when the number of words is over the configured maximum, maxwords. This message is displayed visually and through assistive technologies. The component will replace the %{count} placeholder with the number of characters above the maximum. This is a pluralised list of messages.

Options for countMessage
Name Type Description
classes string

Classes to add to the count message.

Options for formGroup
Name Type Description
classes string

Classes to add to the form group (for example to show error state for the whole group).

attributes object

HTML attributes (for example data attributes) to add to the form group.

beforeInput object

Content to add before the textarea used by the character count component.

See beforeInput.

afterInput object

Content to add after the textarea used by the character count component.

See afterInput.

Options for afterInput
Name Type Description
text string Required.

Text to add after the textarea. If html is provided, the text option will be ignored.

html string Required.

HTML to add after the textarea. If html is provided, the text option will be ignored.

Options for beforeInput
Name Type Description
text string Required.

Text to add before the textarea. If html is provided, the text option will be ignored.

html string Required.

HTML to add before the textarea. If html is provided, the text option will be ignored.

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 option 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 option 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.

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 option 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 option 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.

{% from "moduk/components/character-count/macro.njk" import modukCharacterCount -%}

{{ modukCharacterCount({
name: "with-hint",
id: "with-hint",
maxlength: 200,
label: {
text: "Can you provide more detail?",
classes: "govuk-label--l",
isPageHeading: true
},
hint: {
text: "Do not include personal information like your service number."
}
}) -}}

See GOV.UK character count component guidance for:

  • when to use this component
  • when not to use it

How it works

See GOV.UK character count component guidance for detail on how this component works.

If you’re asking one question on the page

If you’re asking just one question per page as recommended, you can set the contents of the <label> as the page heading.

Do not include personal information like your service number.
You can enter up to 200 characters
<div
class="govuk-form-group govuk-character-count"
data-module="govuk-character-count"
data-maxlength="200"
>

<h1 class="govuk-label-wrapper">
<label class="govuk-label govuk-label--l" for="with-page-heading">
Can you provide more detail?
</label>
</h1>
<div id="with-page-heading-hint" class="govuk-hint">
Do not include personal information like your service number.
</div>
<textarea
class="govuk-textarea govuk-js-character-count"
id="with-page-heading"
name="with-page-heading"
rows="5"
aria-describedby="with-page-heading-info with-page-heading-hint"
>
</textarea>
<div
id="with-page-heading-info"
class="govuk-hint govuk-character-count__message"
>

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 option 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 option 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.

The label used by the character count component.

See label.

hint object

Can be used to add a hint to the character count component.

See hint.

errorMessage object

Can be used to add an error message to the character count component. The error message component will not display if you use a falsy value for errorMessage, for example false or null.

See error message.

formGroup object

Additional options for the form group containing the character count component.

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

Additional options for the count message used by the character count component.

See countMessage.

textareaDescriptionText string

Message made available to assistive technologies to describe that the component accepts only a limited amount of content. It is visible on the page when JavaScript is unavailable. The component will replace the %{count} placeholder with the value of the maxlength or maxwords parameter.

charactersUnderLimitText object

Message displayed when the number of characters is under the configured maximum, maxlength. This message is displayed visually and through assistive technologies. The component will replace the %{count} placeholder with the number of remaining characters. This is a pluralised list of messages.

charactersAtLimitText string

Message displayed when the number of characters reaches the configured maximum, maxlength. This message is displayed visually and through assistive technologies.

charactersOverLimitText object

Message displayed when the number of characters is over the configured maximum, maxlength. This message is displayed visually and through assistive technologies. The component will replace the %{count} placeholder with the number of characters above the maximum. This is a pluralised list of messages.

wordsUnderLimitText object

Message displayed when the number of words is under the configured maximum, maxwords. This message is displayed visually and through assistive technologies. The component will replace the %{count} placeholder with the number of remaining words. This is a pluralised list of messages.

wordsAtLimitText string

Message displayed when the number of words reaches the configured maximum, maxwords. This message is displayed visually and through assistive technologies.

wordsOverLimitText object

Message displayed when the number of words is over the configured maximum, maxwords. This message is displayed visually and through assistive technologies. The component will replace the %{count} placeholder with the number of characters above the maximum. This is a pluralised list of messages.

Options for countMessage
Name Type Description
classes string

Classes to add to the count message.

Options for formGroup
Name Type Description
classes string

Classes to add to the form group (for example to show error state for the whole group).

attributes object

HTML attributes (for example data attributes) to add to the form group.

beforeInput object

Content to add before the textarea used by the character count component.

See beforeInput.

afterInput object

Content to add after the textarea used by the character count component.

See afterInput.

Options for afterInput
Name Type Description
text string Required.

Text to add after the textarea. If html is provided, the text option will be ignored.

html string Required.

HTML to add after the textarea. If html is provided, the text option will be ignored.

Options for beforeInput
Name Type Description
text string Required.

Text to add before the textarea. If html is provided, the text option will be ignored.

html string Required.

HTML to add before the textarea. If html is provided, the text option will be ignored.

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 option 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 option 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.

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 option 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 option 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.

{% from "moduk/components/character-count/macro.njk" import modukCharacterCount %}

{{ modukCharacterCount({
name: "with-page-heading",
id: "with-page-heading",
maxlength: 200,
label: {
text: "Can you provide more detail?",
classes: "govuk-label--l",
isPageHeading: true
},
hint: {
text: "Do not include personal information like your service number."
}
}) }}

If you’re asking more than one question on the page

If you’re asking more than one question on the page, do not set the contents of the <label> as the page heading.

You can enter up to 200 characters
<div
class="govuk-form-group govuk-character-count"
data-module="govuk-character-count"
data-maxlength="200"
>

<label class="govuk-label" for="not-as-page-heading">
Describe the nature of your event
</label>
<textarea
class="govuk-textarea govuk-js-character-count"
id="not-as-page-heading"
name="not-as-page-heading"
rows="5"
aria-describedby="not-as-page-heading-info"
>
</textarea>
<div
id="not-as-page-heading-info"
class="govuk-hint govuk-character-count__message"
>

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 option 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 option 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.

The label used by the character count component.

See label.

hint object

Can be used to add a hint to the character count component.

See hint.

errorMessage object

Can be used to add an error message to the character count component. The error message component will not display if you use a falsy value for errorMessage, for example false or null.

See error message.

formGroup object

Additional options for the form group containing the character count component.

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

Additional options for the count message used by the character count component.

See countMessage.

textareaDescriptionText string

Message made available to assistive technologies to describe that the component accepts only a limited amount of content. It is visible on the page when JavaScript is unavailable. The component will replace the %{count} placeholder with the value of the maxlength or maxwords parameter.

charactersUnderLimitText object

Message displayed when the number of characters is under the configured maximum, maxlength. This message is displayed visually and through assistive technologies. The component will replace the %{count} placeholder with the number of remaining characters. This is a pluralised list of messages.

charactersAtLimitText string

Message displayed when the number of characters reaches the configured maximum, maxlength. This message is displayed visually and through assistive technologies.

charactersOverLimitText object

Message displayed when the number of characters is over the configured maximum, maxlength. This message is displayed visually and through assistive technologies. The component will replace the %{count} placeholder with the number of characters above the maximum. This is a pluralised list of messages.

wordsUnderLimitText object

Message displayed when the number of words is under the configured maximum, maxwords. This message is displayed visually and through assistive technologies. The component will replace the %{count} placeholder with the number of remaining words. This is a pluralised list of messages.

wordsAtLimitText string

Message displayed when the number of words reaches the configured maximum, maxwords. This message is displayed visually and through assistive technologies.

wordsOverLimitText object

Message displayed when the number of words is over the configured maximum, maxwords. This message is displayed visually and through assistive technologies. The component will replace the %{count} placeholder with the number of characters above the maximum. This is a pluralised list of messages.

Options for countMessage
Name Type Description
classes string

Classes to add to the count message.

Options for formGroup
Name Type Description
classes string

Classes to add to the form group (for example to show error state for the whole group).

attributes object

HTML attributes (for example data attributes) to add to the form group.

beforeInput object

Content to add before the textarea used by the character count component.

See beforeInput.

afterInput object

Content to add after the textarea used by the character count component.

See afterInput.

Options for afterInput
Name Type Description
text string Required.

Text to add after the textarea. If html is provided, the text option will be ignored.

html string Required.

HTML to add after the textarea. If html is provided, the text option will be ignored.

Options for beforeInput
Name Type Description
text string Required.

Text to add before the textarea. If html is provided, the text option will be ignored.

html string Required.

HTML to add before the textarea. If html is provided, the text option will be ignored.

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 option 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 option 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.

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 option 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 option 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.

{% from "moduk/components/character-count/macro.njk" import modukCharacterCount -%}

{{ modukCharacterCount({
name: "not-as-page-heading",
id: "not-as-page-heading",
maxlength: 200,
label: {
text: "Describe the nature of your event"
}
}) -}}

Error messages

Error messages should be styled like this:

Do not include personal information like your service number.

Error: Detail must be 200 characters or less

You can enter up to 200 characters
<div
class="govuk-form-group govuk-form-group--error govuk-character-count"
data-module="govuk-character-count"
data-maxlength="200"
>

<h1 class="govuk-label-wrapper">
<label class="govuk-label govuk-label--l" for="exceeding-characters">
Can you provide more detail?
</label>
</h1>
<div id="exceeding-characters-hint" class="govuk-hint">
Do not include personal information like your service number.
</div>
<p id="exceeding-characters-error" class="govuk-error-message">
<span class="govuk-visually-hidden">Error:</span> Detail must be 200
characters or less
</p>
<textarea
class="govuk-textarea govuk-textarea--error govuk-js-character-count"
id="exceeding-characters"
name="exceeding"
rows="5"
aria-describedby="exceeding-characters-info exceeding-characters-hint exceeding-characters-error"
>

I would like to request my service record and have a question about what proof of address is acceptable. The guidance doesn’t say if a digital bank statement will be accepted. Can someone please confirm if</textarea
>

<div
id="exceeding-characters-info"
class="govuk-hint govuk-character-count__message"
>

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 option 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 option 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.

The label used by the character count component.

See label.

hint object

Can be used to add a hint to the character count component.

See hint.

errorMessage object

Can be used to add an error message to the character count component. The error message component will not display if you use a falsy value for errorMessage, for example false or null.

See error message.

formGroup object

Additional options for the form group containing the character count component.

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

Additional options for the count message used by the character count component.

See countMessage.

textareaDescriptionText string

Message made available to assistive technologies to describe that the component accepts only a limited amount of content. It is visible on the page when JavaScript is unavailable. The component will replace the %{count} placeholder with the value of the maxlength or maxwords parameter.

charactersUnderLimitText object

Message displayed when the number of characters is under the configured maximum, maxlength. This message is displayed visually and through assistive technologies. The component will replace the %{count} placeholder with the number of remaining characters. This is a pluralised list of messages.

charactersAtLimitText string

Message displayed when the number of characters reaches the configured maximum, maxlength. This message is displayed visually and through assistive technologies.

charactersOverLimitText object

Message displayed when the number of characters is over the configured maximum, maxlength. This message is displayed visually and through assistive technologies. The component will replace the %{count} placeholder with the number of characters above the maximum. This is a pluralised list of messages.

wordsUnderLimitText object

Message displayed when the number of words is under the configured maximum, maxwords. This message is displayed visually and through assistive technologies. The component will replace the %{count} placeholder with the number of remaining words. This is a pluralised list of messages.

wordsAtLimitText string

Message displayed when the number of words reaches the configured maximum, maxwords. This message is displayed visually and through assistive technologies.

wordsOverLimitText object

Message displayed when the number of words is over the configured maximum, maxwords. This message is displayed visually and through assistive technologies. The component will replace the %{count} placeholder with the number of characters above the maximum. This is a pluralised list of messages.

Options for countMessage
Name Type Description
classes string

Classes to add to the count message.

Options for formGroup
Name Type Description
classes string

Classes to add to the form group (for example to show error state for the whole group).

attributes object

HTML attributes (for example data attributes) to add to the form group.

beforeInput object

Content to add before the textarea used by the character count component.

See beforeInput.

afterInput object

Content to add after the textarea used by the character count component.

See afterInput.

Options for afterInput
Name Type Description
text string Required.

Text to add after the textarea. If html is provided, the text option will be ignored.

html string Required.

HTML to add after the textarea. If html is provided, the text option will be ignored.

Options for beforeInput
Name Type Description
text string Required.

Text to add before the textarea. If html is provided, the text option will be ignored.

html string Required.

HTML to add before the textarea. If html is provided, the text option will be ignored.

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 option 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 option 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.

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 option 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 option 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.

{% from "moduk/components/character-count/macro.njk" import modukCharacterCount -%}

{{ modukCharacterCount({
id: "exceeding-characters",
name: "exceeding",
maxlength: 200,
value: "I would like to request my service record and have a question about what proof of address is acceptable. The guidance doesn’t say if a digital bank statement will be accepted. Can someone please confirm if",
label: {
text: "Can you provide more detail?",
classes: "govuk-label--l",
isPageHeading: true
},
hint: {
text: "Do not include personal information like your service number."
},
errorMessage: {
text: "Detail must be 200 characters or less"
}
}) -}}

Changes from the GOV.UK version

MOD.UK Design System components are closely based on GOV.UK Design System components.

For this component we’ve changed the font.

Have you tested this component?

Let us know how we could improve this component or share your user research findings.

Email the MOD.UK Design System team at design-system@digital.mod.uk

Need help?

Email the MOD.UK Design System team at design-system@digital.mod.uk if you have questions or feedback.