Text input
<div id="event-name" class="govuk-form-group">
<label for="event-name-input" class="govuk-label">
<h1 class="govuk-heading-l">
What is the name of the event?
</h1>
</label>
<input
name="event-name"
id="event-name-input"
class="govuk-input"
type="text"
/>
</div>
<TextInput
label={
<h1 className="govuk-heading-l">
What is the name of the event?
</h1>
}
name="event-name"
/>
Props
Name | Type | Default | Description |
---|---|---|---|
id | string | 'id' attribute to place on the base HTML element | |
classBlock | string | Block name override in BEM style classes applied to all elements | |
classModifiers | other | BEM style modifiers to apply to the base HTML element | |
className | string | Extra classes to apply to the base HTML element | |
accept | string | ||
alt | string | ||
autoComplete | other | ||
capture | other | ||
checked | boolean | ||
disabled | boolean | ||
form | string | ||
formAction | string | ||
formEncType | string | ||
formMethod | string | ||
formNoValidate | boolean | ||
formTarget | string | ||
height | other | ||
list | string | ||
max | other | ||
maxLength | number | ||
min | other | ||
minLength | number | ||
multiple | boolean | ||
name | string | ||
pattern | string | ||
placeholder | string | ||
readOnly | boolean | ||
required | boolean | ||
size | number | ||
src | string | ||
step | other | ||
type | other | ||
value | other | ||
width | number | Width of the field in characters (approximate) | |
onChange | other | ||
defaultChecked | boolean | ||
defaultValue | other | ||
suppressContentEditableWarning | boolean | ||
suppressHydrationWarning | boolean | ||
accessKey | string | ||
autoFocus | boolean | ||
contentEditable | other | ||
contextMenu | string | ||
dir | string | ||
draggable | other | ||
enterKeyHint | enum | ||
hidden | boolean | ||
lang | string | ||
nonce | string | ||
slot | string | ||
spellCheck | other | ||
style | other | ||
tabIndex | number | ||
title | string | ||
translate | enum | ||
radioGroup | string | ||
role | other | ||
about | string | ||
content | string | ||
datatype | string | ||
inlist | other | ||
prefix | string | Prefix to show before the field | |
property | string | ||
rel | string | ||
resource | string | ||
rev | string | ||
typeof | string | ||
vocab | string | ||
autoCapitalize | string | ||
autoCorrect | string | ||
autoSave | string | ||
color | string | ||
itemProp | string | ||
itemScope | boolean | ||
itemType | string | ||
itemID | string | ||
itemRef | string | ||
results | number | ||
security | string | ||
unselectable | enum | ||
inputMode | enum | Hints at the type of data that might be entered by the user while editing the element or its contents | |
is | string | Specify that a standard HTML element should behave like a defined custom built-in element | |
aria-activedescendant | string | Identifies the currently active element when DOM focus is on a composite widget, textbox, group, or application. | |
aria-atomic | other | Indicates whether assistive technologies will present all, or only parts of, the changed region based on the change notifications defined by the aria-relevant attribute. | |
aria-autocomplete | enum | Indicates whether inputting text could trigger display of one or more predictions of the user's intended value for an input and specifies how predictions would be presented if they are made. | |
aria-braillelabel | string | Defines a string value that labels the current element, which is intended to be converted into Braille. | |
aria-brailleroledescription | string | Defines a human-readable, author-localized abbreviated description for the role of an element, which is intended to be converted into Braille. | |
aria-busy | other | ||
aria-checked | other | Indicates the current "checked" state of checkboxes, radio buttons, and other widgets. | |
aria-colcount | number | Defines the total number of columns in a table, grid, or treegrid. | |
aria-colindex | number | Defines an element's column index or position with respect to the total number of columns within a table, grid, or treegrid. | |
aria-colindextext | string | Defines a human readable text alternative of aria-colindex. | |
aria-colspan | number | Defines the number of columns spanned by a cell or gridcell within a table, grid, or treegrid. | |
aria-controls | string | Identifies the element (or elements) whose contents or presence are controlled by the current element. | |
aria-current | other | Indicates the element that represents the current item within a container or set of related elements. | |
aria-describedby | string | Identifies the element (or elements) that describes the object. | |
aria-description | string | Defines a string value that describes or annotates the current element. | |
aria-details | string | Identifies the element that provides a detailed, extended description for the object. | |
aria-disabled | other | Indicates that the element is perceivable but disabled, so it is not editable or otherwise operable. | |
aria-dropeffect | enum | Indicates what functions can be performed when a dragged object is released on the drop target. | |
aria-errormessage | string | Identifies the element that provides an error message for the object. | |
aria-expanded | other | Indicates whether the element, or another grouping element it controls, is currently expanded or collapsed. | |
aria-flowto | string | Identifies the next element (or elements) in an alternate reading order of content which, at the user's discretion, allows assistive technology to override the general default of reading in document source order. | |
aria-grabbed | other | Indicates an element's "grabbed" state in a drag-and-drop operation. | |
aria-haspopup | other | Indicates the availability and type of interactive popup element, such as menu or dialog, that can be triggered by an element. | |
aria-hidden | other | Indicates whether the element is exposed to an accessibility API. | |
aria-invalid | other | Indicates the entered value does not conform to the format expected by the application. | |
aria-keyshortcuts | string | Indicates keyboard shortcuts that an author has implemented to activate or give focus to an element. | |
aria-label | string | Defines a string value that labels the current element. | |
aria-labelledby | string | Identifies the element (or elements) that labels the current element. | |
aria-level | number | Defines the hierarchical level of an element within a structure. | |
aria-live | enum | Indicates that an element will be updated, and describes the types of updates the user agents, assistive technologies, and user can expect from the live region. | |
aria-modal | other | Indicates whether an element is modal when displayed. | |
aria-multiline | other | Indicates whether a text box accepts multiple lines of input or only a single line. | |
aria-multiselectable | other | Indicates that the user may select more than one item from the current selectable descendants. | |
aria-orientation | enum | Indicates whether the element's orientation is horizontal, vertical, or unknown/ambiguous. | |
aria-owns | string | Identifies an element (or elements) in order to define a visual, functional, or contextual parent/child relationship between DOM elements where the DOM hierarchy cannot be used to represent the relationship. | |
aria-placeholder | string | Defines a short hint (a word or short phrase) intended to aid the user with data entry when the control has no value. A hint could be a sample value or a brief description of the expected format. | |
aria-posinset | number | Defines an element's number or position in the current set of listitems or treeitems. Not required if all elements in the set are present in the DOM. | |
aria-pressed | other | Indicates the current "pressed" state of toggle buttons. | |
aria-readonly | other | Indicates that the element is not editable, but is otherwise operable. | |
aria-relevant | enum | Indicates what notifications the user agent will trigger when the accessibility tree within a live region is modified. | |
aria-required | other | Indicates that user input is required on the element before a form may be submitted. | |
aria-roledescription | string | Defines a human-readable, author-localized description for the role of an element. | |
aria-rowcount | number | Defines the total number of rows in a table, grid, or treegrid. | |
aria-rowindex | number | Defines an element's row index or position with respect to the total number of rows within a table, grid, or treegrid. | |
aria-rowindextext | string | Defines a human readable text alternative of aria-rowindex. | |
aria-rowspan | number | Defines the number of rows spanned by a cell or gridcell within a table, grid, or treegrid. | |
aria-selected | other | Indicates the current "selected" state of various widgets. | |
aria-setsize | number | Defines the number of items in the current set of listitems or treeitems. Not required if all elements in the set are present in the DOM. | |
aria-sort | enum | Indicates if items in a table or grid are sorted in ascending or descending order. | |
aria-valuemax | number | Defines the maximum allowed value for a range widget. | |
aria-valuemin | number | Defines the minimum allowed value for a range widget. | |
aria-valuenow | number | Defines the current value for a range widget. | |
aria-valuetext | string | Defines the human readable text alternative of aria-valuenow for a range widget. | |
dangerouslySetInnerHTML | other | ||
onCopy | other | ||
onCopyCapture | other | ||
onCut | other | ||
onCutCapture | other | ||
onPaste | other | ||
onPasteCapture | other | ||
onCompositionEnd | other | ||
onCompositionEndCapture | other | ||
onCompositionStart | other | ||
onCompositionStartCapture | other | ||
onCompositionUpdate | other | ||
onCompositionUpdateCapture | other | ||
onFocus | other | ||
onFocusCapture | other | ||
onBlur | other | ||
onBlurCapture | other | ||
onChangeCapture | other | ||
onBeforeInput | other | ||
onBeforeInputCapture | other | ||
onInput | other | ||
onInputCapture | other | ||
onReset | other | ||
onResetCapture | other | ||
onSubmit | other | ||
onSubmitCapture | other | ||
onInvalid | other | ||
onInvalidCapture | other | ||
onLoad | other | ||
onLoadCapture | other | ||
onError | other | ||
onErrorCapture | other | ||
onKeyDown | other | ||
onKeyDownCapture | other | ||
onKeyPress | other | ||
onKeyPressCapture | other | ||
onKeyUp | other | ||
onKeyUpCapture | other | ||
onAbort | other | ||
onAbortCapture | other | ||
onCanPlay | other | ||
onCanPlayCapture | other | ||
onCanPlayThrough | other | ||
onCanPlayThroughCapture | other | ||
onDurationChange | other | ||
onDurationChangeCapture | other | ||
onEmptied | other | ||
onEmptiedCapture | other | ||
onEncrypted | other | ||
onEncryptedCapture | other | ||
onEnded | other | ||
onEndedCapture | other | ||
onLoadedData | other | ||
onLoadedDataCapture | other | ||
onLoadedMetadata | other | ||
onLoadedMetadataCapture | other | ||
onLoadStart | other | ||
onLoadStartCapture | other | ||
onPause | other | ||
onPauseCapture | other | ||
onPlay | other | ||
onPlayCapture | other | ||
onPlaying | other | ||
onPlayingCapture | other | ||
onProgress | other | ||
onProgressCapture | other | ||
onRateChange | other | ||
onRateChangeCapture | other | ||
onResize | other | ||
onResizeCapture | other | ||
onSeeked | other | ||
onSeekedCapture | other | ||
onSeeking | other | ||
onSeekingCapture | other | ||
onStalled | other | ||
onStalledCapture | other | ||
onSuspend | other | ||
onSuspendCapture | other | ||
onTimeUpdate | other | ||
onTimeUpdateCapture | other | ||
onVolumeChange | other | ||
onVolumeChangeCapture | other | ||
onWaiting | other | ||
onWaitingCapture | other | ||
onAuxClick | other | ||
onAuxClickCapture | other | ||
onClick | other | ||
onClickCapture | other | ||
onContextMenu | other | ||
onContextMenuCapture | other | ||
onDoubleClick | other | ||
onDoubleClickCapture | other | ||
onDrag | other | ||
onDragCapture | other | ||
onDragEnd | other | ||
onDragEndCapture | other | ||
onDragEnter | other | ||
onDragEnterCapture | other | ||
onDragExit | other | ||
onDragExitCapture | other | ||
onDragLeave | other | ||
onDragLeaveCapture | other | ||
onDragOver | other | ||
onDragOverCapture | other | ||
onDragStart | other | ||
onDragStartCapture | other | ||
onDrop | other | ||
onDropCapture | other | ||
onMouseDown | other | ||
onMouseDownCapture | other | ||
onMouseEnter | other | ||
onMouseLeave | other | ||
onMouseMove | other | ||
onMouseMoveCapture | other | ||
onMouseOut | other | ||
onMouseOutCapture | other | ||
onMouseOver | other | ||
onMouseOverCapture | other | ||
onMouseUp | other | ||
onMouseUpCapture | other | ||
onSelect | other | ||
onSelectCapture | other | ||
onTouchCancel | other | ||
onTouchCancelCapture | other | ||
onTouchEnd | other | ||
onTouchEndCapture | other | ||
onTouchMove | other | ||
onTouchMoveCapture | other | ||
onTouchStart | other | ||
onTouchStartCapture | other | ||
onPointerDown | other | ||
onPointerDownCapture | other | ||
onPointerMove | other | ||
onPointerMoveCapture | other | ||
onPointerUp | other | ||
onPointerUpCapture | other | ||
onPointerCancel | other | ||
onPointerCancelCapture | other | ||
onPointerEnter | other | ||
onPointerLeave | other | ||
onPointerOver | other | ||
onPointerOverCapture | other | ||
onPointerOut | other | ||
onPointerOutCapture | other | ||
onGotPointerCapture | other | ||
onGotPointerCaptureCapture | other | ||
onLostPointerCapture | other | ||
onLostPointerCaptureCapture | other | ||
onScroll | other | ||
onScrollCapture | other | ||
onWheel | other | ||
onWheelCapture | other | ||
onAnimationStart | other | ||
onAnimationStartCapture | other | ||
onAnimationEnd | other | ||
onAnimationEndCapture | other | ||
onAnimationIteration | other | ||
onAnimationIterationCapture | other | ||
onTransitionEnd | other | ||
onTransitionEndCapture | other | ||
suffix | string | Suffix to show after the field | |
error | other | Error message | |
hint | other | Hint | |
label | other | Label |
When to use this component
Use the text input component when you need to let users enter text that’s no longer than a single line, such as their name or phone number.
When not to use this component
Do not use the text input component if you need to let users enter longer answers that might span multiple lines. In this case, you should use the textarea component.
How it works
All text inputs must have visible labels; placeholder text is not an acceptable replacement for a label as it vanishes when users start typing.
Labels should be aligned above the text input they refer to. They should be short, direct and written in sentence case. Do not use colons at the end of labels.
If you’re asking just one question per page as recommended, you can set the
contents of the <label>
as the page heading. This is good practice as it means
that users of screen readers will only hear the contents once.
Read more about why and how to set legends as headings.
<div id="event-name" class="govuk-form-group">
<label for="event-name-input" class="govuk-label">
<h1 class="govuk-heading-l">
What is the name of the event?
</h1>
</label>
<input
name="event-name"
id="event-name-input"
class="govuk-input"
type="text"
/>
</div>
<TextInput
label={
<h1 className="govuk-heading-l">
What is the name of the event?
</h1>
}
name="event-name"
/>
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. Read more about asking multiple questions on
question pages.
<div id="event-name" class="govuk-form-group">
<label for="event-name-input" class="govuk-label">
What is the name of the event?
</label>
<input
name="event-name"
id="event-name-input"
class="govuk-input"
type="text"
/>
</div>
<TextInput
label="What is the name of the event?"
name="event-name"
/>
Use appropriately-sized text inputs
Help users understand what they should enter by making text inputs the right size for the content they’re intended for.
By default, the width of text inputs is fluid and will fit the full width of the container they are placed into.
If you want to make the input smaller, you can either use a fixed width input, or use the width override classes to create a smaller, fluid width input.
Fixed width inputs
Use fixed width inputs for content that has a specific, known length. Postcode inputs should be postcode-sized, telephone number inputs should be telephone number-sized.
The widths are designed for specific character lengths and to be consistent across a range of browsers. They include extra padding to fit icons that some browsers might insert into the input (for example to show or generate a password).
On fixed width inputs, the width will remain fixed on all screens unless it is wider than the viewport, in which case it will shrink to fit.
<div id="width-20" class="govuk-form-group">
<label for="width-20-input" class="govuk-label">
20 character width
</label>
<input
name="width-20"
id="width-20-input"
class="govuk-input"
style="max-width:40.96ex"
type="text"
/>
</div>
<div id="width-10" class="govuk-form-group">
<label for="width-10-input" class="govuk-label">
10 character width
</label>
<input
name="width-10"
id="width-10-input"
class="govuk-input"
style="max-width:22.86ex"
type="text"
/>
</div>
<div id="width-5" class="govuk-form-group">
<label for="width-5-input" class="govuk-label">
5 character width
</label>
<input
name="width-5"
id="width-5-input"
class="govuk-input"
style="max-width:10.81ex"
type="text"
/>
</div>
<div id="width-4" class="govuk-form-group">
<label for="width-4-input" class="govuk-label">
4 character width
</label>
<input
name="width-4"
id="width-4-input"
class="govuk-input"
style="max-width:9ex"
type="text"
/>
</div>
<div id="width-3" class="govuk-form-group">
<label for="width-3-input" class="govuk-label">
3 character width
</label>
<input
name="width-3"
id="width-3-input"
class="govuk-input"
style="max-width:7.1899999999999995ex"
type="text"
/>
</div>
<div id="width-2" class="govuk-form-group">
<label for="width-2-input" class="govuk-label">
2 character width
</label>
<input
name="width-2"
id="width-2-input"
class="govuk-input"
style="max-width:5.38ex"
type="text"
/>
</div>
<>
<TextInput
label="20 character width"
name="width-20"
width="20"
/>
<TextInput
label="10 character width"
name="width-10"
width="10"
/>
<TextInput
label="5 character width"
name="width-5"
width="5"
/>
<TextInput
label="4 character width"
name="width-4"
width="4"
/>
<TextInput
label="3 character width"
name="width-3"
width="3"
/>
<TextInput
label="2 character width"
name="width-2"
width="2"
/>
</>
Fluid width inputs
Use the width override classes to reduce the width of an input in relation to its parent container, for example, to two-thirds.
Fluid width inputs will resize with the viewport.
<div id="full" class="govuk-form-group">
<label for="full-input" class="govuk-label">
Full width
</label>
<input
name="full"
id="full-input"
class="govuk-input govuk-!-width-full"
type="text"
/>
</div>
<div id="three-quarters" class="govuk-form-group">
<label
for="three-quarters-input"
class="govuk-label"
>
Three-quarters width
</label>
<input
name="three-quarters"
id="three-quarters-input"
class="govuk-input govuk-!-width-three-quarters"
type="text"
/>
</div>
<div id="two-thirds" class="govuk-form-group">
<label for="two-thirds-input" class="govuk-label">
Two-thirds width
</label>
<input
name="two-thirds"
id="two-thirds-input"
class="govuk-input govuk-!-width-two-thirds"
type="text"
/>
</div>
<div id="one-half" class="govuk-form-group">
<label for="one-half-input" class="govuk-label">
One-half width
</label>
<input
name="one-half"
id="one-half-input"
class="govuk-input govuk-!-width-one-half"
type="text"
/>
</div>
<div id="one-third" class="govuk-form-group">
<label for="one-third-input" class="govuk-label">
One-third width
</label>
<input
name="one-third"
id="one-third-input"
class="govuk-input govuk-!-width-one-third"
type="text"
/>
</div>
<div id="one-quarter" class="govuk-form-group">
<label for="one-quarter-input" class="govuk-label">
One-quarter width
</label>
<input
name="one-quarter"
id="one-quarter-input"
class="govuk-input govuk-!-width-one-quarter"
type="text"
/>
</div>
<>
<TextInput
label="Full width"
name="full"
className="govuk-!-width-full"
/>
<TextInput
label="Three-quarters width"
name="three-quarters"
className="govuk-!-width-three-quarters"
/>
<TextInput
label="Two-thirds width"
name="two-thirds"
className="govuk-!-width-two-thirds"
/>
<TextInput
label="One-half width"
name="one-half"
className="govuk-!-width-one-half"
/>
<TextInput
label="One-third width"
name="one-third"
className="govuk-!-width-one-third"
/>
<TextInput
label="One-quarter width"
name="one-quarter"
className="govuk-!-width-one-quarter"
/>
</>
Hint text
Use hint for help that’s relevant to the majority of users, like how their information will be used, or where to find it.
<div id="event-name" class="govuk-form-group">
<label for="event-name-input" class="govuk-label">
<h1 class="govuk-heading-l">
What is the name of the event?
</h1>
</label>
<div id="event-name-hint" class="govuk-hint">
The name you’ll use on promotional material.
</div>
<input
name="event-name"
aria-describedby="event-name-hint"
id="event-name-input"
class="govuk-input"
type="text"
/>
</div>
<TextInput
label={
<h1 className="govuk-heading-l">
What is the name of the event?
</h1>
}
name="event-name"
hint="The name you’ll use on promotional material."
/>
When not to use hint text
Do not use long paragraphs and lists in hint text. Screen readers read out the entire text when users interact with the form element. This could frustrate users if the text is long.
Avoid links
Do not include links within hint text. While screen readers will read out the link text when describing the field, they will not tell users that the text is a link.
Numbers
Asking for whole numbers
If you’re asking the user to enter a whole number and you want to bring up the
numeric keypad on a mobile device, set the inputMode
attribute to numeric
and
the pattern
attribute to [0-9]*
. See how to do this in the HTML and Nunjucks
tabs in the following example.
<div id="account-number" class="govuk-form-group">
<label
for="account-number-input"
class="govuk-label"
>
<h1 class="govuk-heading-l">
What is your account number?
</h1>
</label>
<div id="account-number-hint" class="govuk-hint">
Must be between 6 and 8 digits long
</div>
<input
name="account-number"
inputmode="numeric"
pattern="[0-9]*"
aria-describedby="account-number-hint"
id="account-number-input"
class="govuk-input"
style="max-width:22.86ex"
type="text"
/>
</div>
<TextInput
label={
<h1 className="govuk-heading-l">
What is your account number?
</h1>
}
name="account-number"
hint="Must be between 6 and 8 digits long"
inputMode="numeric"
pattern="[0-9]*"
width={10}
/>
You should also turn off HTML5 validation to prevent browsers from validating
the pattern
attribute.
There is specific guidance on how to ask for:
Asking for decimal numbers
If you’re asking the user to enter a number that might include decimal places,
use input type="text"
without inputMode
or pattern
attributes.
Do not set the inputMode
attribute to decimal as it causes some devices to
bring up a keypad without a key for the decimal
separator.
<div id="weight" class="govuk-form-group">
<label for="weight-input" class="govuk-label">
Weight, in kilograms
</label>
<div class="govuk-input__wrapper">
<input
name="weight"
id="weight-input"
class="govuk-input"
style="max-width:10.81ex"
type="text"
/>
<div
class="govuk-input__suffix"
aria-hidden="true"
>
kg
</div>
</div>
</div>
<TextInput
label="Weight, in kilograms"
name="weight"
width={5}
suffix="kg"
/>
Avoid using inputs with a type of number
Do not use <input type="number">
unless your user research shows that there’s
a need for it. With <input type="number">
there’s a risk of users accidentally
incrementing a number when they’re trying to do something else - for example,
scroll up or down the page. And if the user tries to enter something that’s not
a number, there’s no explicit feedback about what they’re doing wrong.
Prefixes and suffixes
Use prefixes and suffixes to help users enter things like currencies and measurements.
<div id="cost-per-item" class="govuk-form-group">
<label
for="cost-per-item-input"
class="govuk-label"
>
<h1 class="govuk-heading-l">
What is the cost per item, in pounds?
</h1>
</label>
<div class="govuk-input__wrapper">
<div
class="govuk-input__prefix"
aria-hidden="true"
>
£
</div>
<input
name="cost-per-item"
id="cost-per-item-input"
class="govuk-input"
style="max-width:10.81ex"
type="text"
/>
<div
class="govuk-input__suffix"
aria-hidden="true"
>
per item
</div>
</div>
</div>
<TextInput
label={
<h1 className="govuk-heading-l">
What is the cost per item, in pounds?
</h1>
}
name="cost-per-item"
width={5}
prefix="£"
suffix="per item"
/>
Prefixes and suffixes are useful when there’s a commonly understood symbol or abbreviation for the type of information you’re asking for. Do not rely on prefixes or suffixes alone, because screen readers will not read them out.
If you need a specific type of information, say so in the input label or hint text as well. For example, put ‘Cost, in pounds’ in the input label and use the ‘£’ symbol in the prefix.
Position prefixes and suffixes so that they’re outside of their input. This is to avoid interfering with some browsers that might insert an icon into the input (for example to show or generate a password).
Some users may miss that the input already has a suffix or prefix, and enter a prefix or suffix into the input. Allow for this in your validation and do not show an error.
Text inputs with a prefix
<div id="cost" class="govuk-form-group">
<label for="cost-input" class="govuk-label">
<h1 class="govuk-heading-l">
What is the cost in pounds?
</h1>
</label>
<div class="govuk-input__wrapper">
<div
class="govuk-input__prefix"
aria-hidden="true"
>
£
</div>
<input
name="cost"
id="cost-input"
class="govuk-input"
style="max-width:10.81ex"
type="text"
/>
</div>
</div>
<TextInput
label={
<h1 className="govuk-heading-l">
What is the cost in pounds?
</h1>
}
name="cost"
width={5}
prefix="£"
/>
Text inputs with a suffix
<div id="weight" class="govuk-form-group">
<label for="weight-input" class="govuk-label">
<h1 class="govuk-heading-l">
What is the weight in kilograms?
</h1>
</label>
<div class="govuk-input__wrapper">
<input
name="weight"
id="weight-input"
class="govuk-input"
style="max-width:10.81ex"
type="text"
/>
<div
class="govuk-input__suffix"
aria-hidden="true"
>
kg
</div>
</div>
</div>
<TextInput
label={
<h1 className="govuk-heading-l">
What is the weight in kilograms?
</h1>
}
name="weight"
width={5}
suffix="kg"
/>
Use the autoComplete prop
Use the autoComplete
attribute / prop on text inputs to help users complete
forms more quickly. This lets you specify an input’s purpose so browsers can
autofill the information on a user’s behalf if they’ve entered it previously.
For example, to enable autofill on a postcode field, set the autoComplete
prop
to postal-code
. See how to do this in the HTML and Nunjucks tabs in the
following example.
<div id="postcode" class="govuk-form-group">
<label for="postcode-input" class="govuk-label">
Postcode
</label>
<input
name="postcode"
autocomplete="postal-code"
id="postcode-input"
class="govuk-input"
style="max-width:22.86ex"
type="text"
/>
</div>
<TextInput
label="Postcode"
name="postcode"
width="10"
autoComplete="postal-code"
/>
If you are working in production and there is a relevant input purpose, you’ll
need to use the autoComplete
attribute to meet WCAG 2.1 Level AA.
You will not normally need to use the autoComplete
attribute in prototypes, as
users will not generally be using their own devices.
Do not disable copy and paste
Users often need to copy and paste information into a text input, so do not stop them from doing this.
How and when to spellcheck a user’s input
Sometimes, browsers will spellcheck the information a user puts into a text input. If a user enters something which is recognised as a spelling error, sighted users will see a red line under the word.
If you are asking users for information which is not appropriate to spellcheck, like a reference number, name, email address or National Insurance number, disable the spellcheck.
To do this set the spellCheck
attribute to false
as shown in this example.
Browsers do not consistently spellcheck user’s input by default. If you are
asking a question where spellcheck would be useful, set the spellCheck
attribute to true
.
<div id="description" class="govuk-form-group">
<label for="description-input" class="govuk-label">
Description
</label>
<input
name="description"
spellcheck="true"
id="description-input"
class="govuk-input"
type="text"
/>
</div>
<TextInput
label="Description"
name="description"
spellCheck
/>
Error messages
Error messages should be styled like this:
<div
id="event-name"
class="govuk-form-group govuk-form-group--error"
>
<label for="event-name-input" class="govuk-label">
<h1 class="govuk-heading-l">
What is the name of the event?
</h1>
</label>
<div id="event-name-hint" class="govuk-hint">
The name you’ll use on promotional material.
</div>
<p
id="event-name-error"
class="govuk-error-message"
>
<span class="govuk-visually-hidden">Error:</span>
Enter an event name
</p>
<input
name="event-name"
aria-describedby="event-name-hint event-name-error"
id="event-name-input"
class="govuk-input govuk-input--error"
type="text"
/>
</div>
<TextInput
label={
<h1 className="govuk-heading-l">
What is the name of the event?
</h1>
}
name="event-name"
hint="The name you’ll use on promotional material."
error="Enter an event name"
/>
If the input has a prefix or a suffix
<div
id="cost-per-item"
class="govuk-form-group govuk-form-group--error"
>
<label
for="cost-per-item-input"
class="govuk-label"
>
<h1 class="govuk-heading-l">
What is the cost per item, in pounds?
</h1>
</label>
<p
id="cost-per-item-error"
class="govuk-error-message"
>
<span class="govuk-visually-hidden">Error:</span>
Enter a cost per item, in pounds
</p>
<div class="govuk-input__wrapper">
<div
class="govuk-input__prefix"
aria-hidden="true"
>
£
</div>
<input
name="cost-per-item"
aria-describedby="cost-per-item-error"
id="cost-per-item-input"
class="govuk-input govuk-input--error"
style="max-width:10.81ex"
type="text"
/>
<div
class="govuk-input__suffix"
aria-hidden="true"
>
per item
</div>
</div>
</div>
<TextInput
label={
<h1 className="govuk-heading-l">
What is the cost per item, in pounds?
</h1>
}
name="cost-per-item"
width={5}
prefix="£"
suffix="per item"
error="Enter a cost per item, in pounds"
/>
Make sure errors follow the guidance in error message and have specific error messages for specific error states.
If the input is empty
Say ‘Enter [whatever it is]’. For example, ‘Enter your first name’.
If the input is too long
Say ‘[whatever it is] must be [number] characters or fewer’. For example, ‘Address line 1 must be 35 characters or fewer’.
If the input is too short
Say ‘[whatever it is] must be [number] characters or more’. For example, ‘Full name must be 2 characters or more’.
If the input is too long or too short
Say ‘[whatever it is] must be between [number] and [number] characters’. For example, ‘Last name must be between 2 and 35 characters’.
If the input uses characters that are not allowed and you know what the characters are
Say ‘[whatever it is] must not include [characters]’. For example, ‘Town or city must not include è and £’.
If the input uses characters that are not allowed and you do not know what the characters are
Say ‘[whatever it is] must only include [list of allowed characters]’. For example, ‘Full name must only include letters a to z, hyphens, spaces and apostrophes’.
If the input is not a number
Say ‘[whatever it is] must be a number [optional example]’. For example, ‘Hours worked a week must be a number, like 30’.
If the input is not a whole number
Say ‘[whatever it is] must be a whole number [optional example]’. For example, ‘Hours worked a week must be a whole number, like 30’.
If the number is too low
Say ‘[whatever it is] must be [lowest] or more’. For example, ‘Hours worked a week must be 16 or more’.
If the number is too high
Say ‘[whatever it is] must be [highest] or fewer’. For example, ‘Hours worked a week must be 99 or fewer’.
If the input must be between 2 numbers
Say ‘[whatever it is] must be between [lowest] and [highest]’. For example, ‘Hours worked a week must be between 16 and 99’.
If the input is not an amount of money and the field allows decimals
Say ‘[whatever it is] must be an amount of money [optional example that includes decimals and non-decimals]’. For example, ‘How much you earn an hour must be an amount of money, like 7.50 or 8’.
If the input is not an amount of money and the field needs decimals
Say ‘[whatever it is] must be an amount of money [optional example that includes decimals]’. For example, ‘How much you earn an hour must be an amount of money, like 7.50 or 8.00’.
If the input is an amount of money that needs decimals
Say ‘[whatever it is] must include pence, like 123.45 or 156.00’. For example, ‘How much you earn a week must include pence, like 123.45 or 156.00’.
If the input is an amount of money that must not have decimals
Say ‘[whatever it is] must not include pence, like 123 or 156’. For example, ‘How much you earn a week must not include pence, like 123 or 156’.