Skip to main content

This is a draft (version 0.5.0). The National Information Technology Agency (NITA) plans to make GGDS official guidance for government websites after independent accessibility testing. What this means

Ghana Government Design System Design system for Government of Ghana websites – home Search

Forms

Character count

Tells people how many characters they can enter in a textarea, and how many they have left as they type.

When to use

  • When there is a real limit on the length of an answer, such as a system that stores no more than 500 characters.

When not to use

  • To make people write less when there is no real limit. Ask a clearer question instead.
  • On a one-line text input.

How to use

  • Put ggds-character-count on the field and the limit in data-ggds-max. Add the limit as a hint after the textarea, and name it in the textarea's aria-describedby.
  • Do not put maxlength on the textarea: it silently cuts off what people paste. Let them write more, show that it is too long, and check the length on the server.
  • With the GGDS script, the hint becomes a count that updates as people type ("You have 120 characters remaining", "You have 12 characters too many").
  • In Django, a CharField with max_length and a Textarea gets a character count from the GGDS form renderer.
  • The count's text can be translated: data-ggds-remaining-one, data-ggds-remaining-other, data-ggds-over-one and data-ggds-over-other on the field, with {count} for the number. The Django and React parts set them from GGDS's text in the page's language.

Accessibility

What GGDS does

  • The count is shown at once, but screen readers hear it only when people pause typing, so it does not interrupt every key.
  • Too many characters is shown in words, in red and bold, never by colour alone.
  • Without JavaScript, people still see the limit as a hint.

What you need to do

  • Check the length on the server and show an error that says the limit, as in the second example.

Example: Character count

Open this example on its own page

HTML code for “Character count”
HTML
<div class="ggds-field ggds-character-count" data-ggds-module="character-count" data-ggds-max="500">
  <label class="ggds-label" for="details">Describe the problem</label>
  <p class="ggds-hint" id="details-hint">Include dates, amounts and any reference numbers</p>
  <textarea class="ggds-textarea" id="details" name="details" rows="5" aria-describedby="details-hint details-info"></textarea>
  <p class="ggds-hint ggds-character-count__message" id="details-info">You can enter up to 500 characters</p>
</div>
JSX (React)
<div className="ggds-field ggds-character-count" data-ggds-module="character-count" data-ggds-max="500">
  <label className="ggds-label" htmlFor="details">Describe the problem</label>
  <p className="ggds-hint" id="details-hint">Include dates, amounts and any reference numbers</p>
  <textarea className="ggds-textarea" id="details" name="details" rows={5} aria-describedby="details-hint details-info"></textarea>
  <p className="ggds-hint ggds-character-count__message" id="details-info">You can enter up to 500 characters</p>
</div>

Example: Too many characters, after sending

Open this example on its own page

HTML code for “Too many characters, after sending”
HTML
<div class="ggds-field ggds-field--error ggds-character-count" data-ggds-module="character-count" data-ggds-max="100">
  <label class="ggds-label" for="summary">Summarise the problem</label>
  <p class="ggds-error-message" id="summary-error"><span class="ggds-visually-hidden">Error:</span> The summary must be 100 characters or fewer</p>
  <textarea class="ggds-textarea" id="summary" name="summary" rows="3" aria-describedby="summary-error summary-info" aria-invalid="true">My mobile money transfer of GH₵ 250 on 3 November did not arrive, and the agent says the network has the money.</textarea>
  <p class="ggds-hint ggds-character-count__message" id="summary-info">You can enter up to 100 characters</p>
</div>
JSX (React)
<div className="ggds-field ggds-field--error ggds-character-count" data-ggds-module="character-count" data-ggds-max="100">
  <label className="ggds-label" htmlFor="summary">Summarise the problem</label>
  <p className="ggds-error-message" id="summary-error"><span className="ggds-visually-hidden">Error:</span> The summary must be 100 characters or fewer</p>
  <textarea className="ggds-textarea" id="summary" name="summary" rows={3} aria-describedby="summary-error summary-info" aria-invalid="true">My mobile money transfer of GH₵ 250 on 3 November did not arrive, and the agent says the network has the money.</textarea>
  <p className="ggds-hint ggds-character-count__message" id="summary-info">You can enter up to 100 characters</p>
</div>

Use it

Stylesheet
ggds.css includes it. To load only the parts you use: core.css, then components/field.css (1.0 KB with gzip, minified), which also holds text inputs, radios and checkboxes.
JavaScript
Optional: @ggds/js counts as people type and tells screen readers the count when they pause. Without it, the limit is a hint and the server checks the length.

HTML

Copy the HTML under each example above. It works in any website.

Astro

No Astro component: copy the HTML.

React and Next.js

No React component: switch the HTML under an example to JSX (React) and copy it.

Django

With ggds-django ({% load ggds %}):

Text
class ComplaintForm(forms.Form):
    details = forms.CharField(max_length=500, widget=forms.Textarea)  # GGDSFormRenderer adds the count

Classes and states

Classes for character count
ClassWhat it does
.ggds-character-countOn the field: a textarea with a limit. data-ggds-max gives the limit.
.ggds-character-count__messageThe limit, as a hint ("You can enter up to 500 characters").
.ggds-character-count__status--overAdded by the script when there are too many characters.
.ggds-character-count__textarea--overAdded by the script to the textarea when there are too many characters.

Accessibility tests

Automated, on every change

These run each time GGDS changes. A change that fails one is not accepted.

  • WCAG 2.2 A and AA rules (axe), at desktop and phone widths, on every example
  • No sideways scrolling on a 320px screen, also with a wide font, on every example
  • Every approved colour pair meets its contrast minimum, for every institution theme
  • No inline scripts or styles, so it runs under a strict Content Security Policy
  • Behaviour test: “character count updates as people type and tells screen readers when they pause”
  • Behaviour test: “no horizontal scrolling at 320px”
  • Behaviour test: “no horizontal scrolling at 320px with a wide font”

By people, with assistive technology

Automated tests find only some of the barriers people meet. These checks are still to be done.

Manual accessibility checks for character count
CheckResultNotes
Keyboard onlyNot tested yetWaiting for the external audit with disabled people (decision D-09).
NVDA screen reader with Firefox or Chrome (Windows)Not tested yetWaiting for the external audit with disabled people (decision D-09).
JAWS screen reader with Chrome (Windows)Not tested yetWaiting for the external audit with disabled people (decision D-09).
VoiceOver screen reader with Safari (iPhone)Not tested yetWaiting for the external audit with disabled people (decision D-09).
TalkBack screen reader with Chrome (Android)Not tested yetWaiting for the external audit with disabled people (decision D-09).
Zoom to 400%, and increased text spacingNot tested yetWaiting for the external audit with disabled people (decision D-09).
Windows contrast themes (forced colours)Not tested yetWaiting for the external audit with disabled people (decision D-09).
Voice control (Voice Access or Dragon)Not tested yetWaiting for the external audit with disabled people (decision D-09).

Changes

Changes to character count, newest first
VersionChange
0.4.0New components: back link, side navigation, contents list ("On this page"), language switcher, phase banner, cookie banner and character count, in HTML, Django, React and (except the character count) Astro. All are alpha: tested automatically, not yet on a live website. The cookie banner needs no JavaScript: its buttons send a form, and both choices look the same.
0.4.0Django: a CharField with max_length and a Textarea now gets a character count, and no maxlength, which silently cut off pasted text.
0.1.0Added.

Every change, in What’s new

Back to top

Something wrong, unclear or missing? Report a problem with this page or suggest a change to it on GitHub. You need a GitHub account.