Forms
Code input
One box for a code people receive by SMS or email, which their phone can fill in for them.
When to use
- To check a phone number or email address, or as the second step of signing in.
When not to use
- For a mobile money PIN or a bank card's security code: people enter those on their phone or with their bank, never on a government website.
How to use
- Use one box, not one box per digit: separate boxes break pasting, phones' automatic filling and screen readers.
- Set
autocomplete="one-time-code",inputmode="numeric"andclass="ggds-input ggds-input--code". Do not usetype="number": it drops leading zeros. - Say where the code went and how long it lasts in the hint: "We sent a 6-digit code to 024 *** 4567. It expires in 10 minutes."
- Accept the code with spaces or dashes, and remove them before you check it.
- Under the field, link to "Send a new code", and to another way to prove who they are, such as by email.
- Say what went wrong, as in the second example: "The code is not right" or "The code has expired. Send a new code".
Accessibility
What GGDS does
- The box is wide enough for the code, with spaced figures that are easy to check.
What you need to do
- Give people enough time, and a way to get a new code (WCAG 2.2.1, timing adjustable).
- Let them paste the code and let the phone fill it in (WCAG 3.3.8).
Example: A code sent by SMS
<div class="ggds-field">
<label class="ggds-label" for="code">Enter the code we sent you</label>
<p class="ggds-hint" id="code-hint">We sent a 6-digit code to 024 *** 4567. It expires in 10 minutes.</p>
<input class="ggds-input ggds-input--code" id="code" name="code" type="text" inputmode="numeric" autocomplete="one-time-code" spellcheck="false" aria-describedby="code-hint">
</div>
<p><a href="#code">Send a new code</a></p>
<button class="ggds-button" type="submit">Continue</button><>
<div className="ggds-field">
<label className="ggds-label" htmlFor="code">Enter the code we sent you</label>
<p className="ggds-hint" id="code-hint">We sent a 6-digit code to 024 *** 4567. It expires in 10 minutes.</p>
<input className="ggds-input ggds-input--code" id="code" name="code" type="text" inputMode="numeric" autoComplete="one-time-code" spellCheck="false" aria-describedby="code-hint" />
</div>
<p><a href="#code">Send a new code</a></p>
<button className="ggds-button" type="submit">Continue</button>
</>Example: A code that has expired
<div class="ggds-field ggds-field--error">
<label class="ggds-label" for="expired-code">Enter the code we sent you</label>
<p class="ggds-hint" id="expired-code-hint">We sent a 6-digit code to 024 *** 4567. It expires in 10 minutes.</p>
<p class="ggds-error-message" id="expired-code-error"><span class="ggds-visually-hidden">Error:</span> The code has expired. Send a new code</p>
<input class="ggds-input ggds-input--code" id="expired-code" name="code" type="text" inputmode="numeric" autocomplete="one-time-code" spellcheck="false" aria-describedby="expired-code-hint expired-code-error" aria-invalid="true" value="482 913">
</div>
<p><a href="#expired-code">Send a new code</a></p>
<button class="ggds-button" type="submit">Continue</button><>
<div className="ggds-field ggds-field--error">
<label className="ggds-label" htmlFor="expired-code">Enter the code we sent you</label>
<p className="ggds-hint" id="expired-code-hint">We sent a 6-digit code to 024 *** 4567. It expires in 10 minutes.</p>
<p className="ggds-error-message" id="expired-code-error"><span className="ggds-visually-hidden">Error:</span> The code has expired. Send a new code</p>
<input className="ggds-input ggds-input--code" id="expired-code" name="code" type="text" inputMode="numeric" autoComplete="one-time-code" spellCheck="false" aria-describedby="expired-code-hint expired-code-error" aria-invalid="true" defaultValue="482 913" />
</div>
<p><a href="#expired-code">Send a new code</a></p>
<button className="ggds-button" type="submit">Continue</button>
</>Use it
- Stylesheet
ggds.cssincludes it. To load only the parts you use:core.css, thencomponents/field.css(1.0 KB with gzip, minified), which also holds text inputs, radios and checkboxes.- JavaScript
- None needed.
HTML
Copy the HTML under each example above. It works in any website.
Astro
With @ggds/astro:
import CodeInput from '@ggds/astro/CodeInput.astro';
---
import CodeInput from '@ggds/astro/CodeInput.astro';
---
<CodeInput id="code" name="code" label="Enter the code we sent you"
hint="We sent a 6-digit code to 024 *** 4567. It expires in 10 minutes." />React and Next.js
With @ggds/react:
import { CodeInput } from '@ggds/react';
<CodeInput id="code" name="code" label="Enter the code we sent you"
hint="We sent a 6-digit code to 024 *** 4567. It expires in 10 minutes." error={errors.code} />Django
With ggds-django ({% load ggds %}):
from ggds_django.forms import OneTimeCodeField
class CodeForm(forms.Form):
code = OneTimeCodeField(label="Enter the code we sent you", digits=6) # removes spaces and dashesClasses and states
| Class | What it does |
|---|---|
.ggds-input--code | On a .ggds-input: a code, with spaced figures that line up. |
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: “code input: one box that phones can fill in, and keeps leading zeros”
By people, with assistive technology
Automated tests find only some of the barriers people meet. These checks are still to be done.
| Check | Result | Notes |
|---|---|---|
| Keyboard only | Not tested yet | Waiting for the external audit with disabled people (decision D-09). |
| NVDA screen reader with Firefox or Chrome (Windows) | Not tested yet | Waiting for the external audit with disabled people (decision D-09). |
| JAWS screen reader with Chrome (Windows) | Not tested yet | Waiting for the external audit with disabled people (decision D-09). |
| VoiceOver screen reader with Safari (iPhone) | Not tested yet | Waiting for the external audit with disabled people (decision D-09). |
| TalkBack screen reader with Chrome (Android) | Not tested yet | Waiting for the external audit with disabled people (decision D-09). |
| Zoom to 400%, and increased text spacing | Not tested yet | Waiting for the external audit with disabled people (decision D-09). |
| Windows contrast themes (forced colours) | Not tested yet | Waiting for the external audit with disabled people (decision D-09). |
| Voice control (Voice Access or Dragon) | Not tested yet | Waiting for the external audit with disabled people (decision D-09). |
Changes
| Version | Change |
|---|---|
| 0.4.0 | New components for services that take people from start to payment, in HTML, React, Astro and Django, each checked element by element against its documentation example and with axe on phones and computers. All are alpha. |
| 0.1.0 | Added. |
Related components
Something wrong, unclear or missing? or suggest a change to it on GitHub. You need a GitHub account.