Text input
<div id="event-name" class="govuk-form-group">
<label
for="event-name-input"
class="govuk-label"
aria-hidden="false"
>
<h1 class="govuk-heading-l">
What is the name of the event?
</h1>
</label>
<input
id="event-name-input"
class="govuk-input"
type="text"
name="event-name"
/>
</div>
<TextInput
label={
<h1 className="govuk-heading-l">
What is the name of the event?
</h1>
}
name="event-name"
/>
Props
|
| string | - | 'id' attribute to place on the base HTML element |
| string | - | Block name override in BEM style classes applied to all elements |
| Modifiers | [] | BEM style modifiers to apply to the base HTML element |
| string | - | Extra classes to apply to the base HTML element |
| string | - | Suffix to show after the field |
| ReactNode | - | Error message |
| ReactNode | - | Hint |
| ReactNode | - | REQUIRED. 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"
aria-hidden="false"
>
<h1 class="govuk-heading-l">
What is the name of the event?
</h1>
</label>
<input
id="event-name-input"
class="govuk-input"
type="text"
name="event-name"
/>
</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"
aria-hidden="false"
>
What is the name of the event?
</label>
<input
id="event-name-input"
class="govuk-input"
type="text"
name="event-name"
/>
</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"
aria-hidden="false"
>
20 character width
</label>
<input
id="width-20-input"
class="govuk-input"
style="max-width:40.96ex"
type="text"
name="width-20"
/>
</div>
<div id="width-10" class="govuk-form-group">
<label
for="width-10-input"
class="govuk-label"
aria-hidden="false"
>
10 character width
</label>
<input
id="width-10-input"
class="govuk-input"
style="max-width:22.86ex"
type="text"
name="width-10"
/>
</div>
<div id="width-5" class="govuk-form-group">
<label
for="width-5-input"
class="govuk-label"
aria-hidden="false"
>
5 character width
</label>
<input
id="width-5-input"
class="govuk-input"
style="max-width:10.81ex"
type="text"
name="width-5"
/>
</div>
<div id="width-4" class="govuk-form-group">
<label
for="width-4-input"
class="govuk-label"
aria-hidden="false"
>
4 character width
</label>
<input
id="width-4-input"
class="govuk-input"
style="max-width:9ex"
type="text"
name="width-4"
/>
</div>
<div id="width-3" class="govuk-form-group">
<label
for="width-3-input"
class="govuk-label"
aria-hidden="false"
>
3 character width
</label>
<input
id="width-3-input"
class="govuk-input"
style="max-width:7.1899999999999995ex"
type="text"
name="width-3"
/>
</div>
<div id="width-2" class="govuk-form-group">
<label
for="width-2-input"
class="govuk-label"
aria-hidden="false"
>
2 character width
</label>
<input
id="width-2-input"
class="govuk-input"
style="max-width:5.38ex"
type="text"
name="width-2"
/>
</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"
aria-hidden="false"
>
Full width
</label>
<input
id="full-input"
class="govuk-input govuk-!-width-full"
type="text"
name="full"
/>
</div>
<div id="three-quarters" class="govuk-form-group">
<label
for="three-quarters-input"
class="govuk-label"
aria-hidden="false"
>
Three-quarters width
</label>
<input
id="three-quarters-input"
class="govuk-input govuk-!-width-three-quarters"
type="text"
name="three-quarters"
/>
</div>
<div id="two-thirds" class="govuk-form-group">
<label
for="two-thirds-input"
class="govuk-label"
aria-hidden="false"
>
Two-thirds width
</label>
<input
id="two-thirds-input"
class="govuk-input govuk-!-width-two-thirds"
type="text"
name="two-thirds"
/>
</div>
<div id="one-half" class="govuk-form-group">
<label
for="one-half-input"
class="govuk-label"
aria-hidden="false"
>
One-half width
</label>
<input
id="one-half-input"
class="govuk-input govuk-!-width-one-half"
type="text"
name="one-half"
/>
</div>
<div id="one-third" class="govuk-form-group">
<label
for="one-third-input"
class="govuk-label"
aria-hidden="false"
>
One-third width
</label>
<input
id="one-third-input"
class="govuk-input govuk-!-width-one-third"
type="text"
name="one-third"
/>
</div>
<div id="one-quarter" class="govuk-form-group">
<label
for="one-quarter-input"
class="govuk-label"
aria-hidden="false"
>
One-quarter width
</label>
<input
id="one-quarter-input"
class="govuk-input govuk-!-width-one-quarter"
type="text"
name="one-quarter"
/>
</div>
<>
<TextInput
className="govuk-!-width-full"
label="Full width"
name="full"
/>
<TextInput
className="govuk-!-width-three-quarters"
label="Three-quarters width"
name="three-quarters"
/>
<TextInput
className="govuk-!-width-two-thirds"
label="Two-thirds width"
name="two-thirds"
/>
<TextInput
className="govuk-!-width-one-half"
label="One-half width"
name="one-half"
/>
<TextInput
className="govuk-!-width-one-third"
label="One-third width"
name="one-third"
/>
<TextInput
className="govuk-!-width-one-quarter"
label="One-quarter width"
name="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"
aria-hidden="false"
>
<h1 class="govuk-heading-l">
What is the name of the event?
</h1>
</label>
<div
id="event-name-hint"
class="govuk-hint"
aria-hidden="false"
>
The name you’ll use on promotional material.
</div>
<input
aria-describedby="event-name-hint"
id="event-name-input"
class="govuk-input"
type="text"
name="event-name"
/>
</div>
<TextInput
hint="The name you’ll use on promotional material."
label={
<h1 className="govuk-heading-l">
What is the name of the event?
</h1>
}
name="event-name"
/>
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"
aria-hidden="false"
>
<h1 class="govuk-heading-l">
What is your account number?
</h1>
</label>
<div
id="account-number-hint"
class="govuk-hint"
aria-hidden="false"
>
Must be between 6 and 8 digits long
</div>
<input
inputmode="numeric"
pattern="[0-9]*"
aria-describedby="account-number-hint"
id="account-number-input"
class="govuk-input"
style="max-width:22.86ex"
type="text"
name="account-number"
/>
</div>
<TextInput
hint="Must be between 6 and 8 digits long"
inputMode="numeric"
label={
<h1 className="govuk-heading-l">
What is your account number?
</h1>
}
name="account-number"
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"
aria-hidden="false"
>
Weight, in kilograms
</label>
<div class="govuk-input__wrapper">
<input
id="weight-input"
class="govuk-input"
style="max-width:10.81ex"
type="text"
name="weight"
/>
<div
class="govuk-input__suffix"
aria-hidden="true"
>
kg
</div>
</div>
</div>
<TextInput
label="Weight, in kilograms"
name="weight"
suffix="kg"
width={5}
/>
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"
aria-hidden="false"
>
<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
id="cost-per-item-input"
class="govuk-input"
style="max-width:10.81ex"
type="text"
name="cost-per-item"
/>
<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"
prefix="£"
suffix="per item"
width={5}
/>
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"
aria-hidden="false"
>
<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
id="cost-input"
class="govuk-input"
style="max-width:10.81ex"
type="text"
name="cost"
/>
</div>
</div>
<TextInput
label={
<h1 className="govuk-heading-l">
What is the cost in pounds?
</h1>
}
name="cost"
prefix="£"
width={5}
/>
Text inputs with a suffix
<div id="weight" class="govuk-form-group">
<label
for="weight-input"
class="govuk-label"
aria-hidden="false"
>
<h1 class="govuk-heading-l">
What is the weight in kilograms?
</h1>
</label>
<div class="govuk-input__wrapper">
<input
id="weight-input"
class="govuk-input"
style="max-width:10.81ex"
type="text"
name="weight"
/>
<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"
suffix="kg"
width={5}
/>
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"
aria-hidden="false"
>
Postcode
</label>
<input
autocomplete="postal-code"
id="postcode-input"
class="govuk-input"
style="max-width:22.86ex"
type="text"
name="postcode"
/>
</div>
<TextInput
autoComplete="postal-code"
label="Postcode"
name="postcode"
width="10"
/>
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.
<div id="name" class="govuk-form-group">
<label
for="name-input"
class="govuk-label"
aria-hidden="false"
>
Name
</label>
<input
spellcheck="false"
id="name-input"
class="govuk-input"
type="text"
name="name"
/>
</div>
<TextInput label="Name" name="name" />
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"
aria-hidden="false"
>
Description
</label>
<input
spellcheck="true"
id="description-input"
class="govuk-input"
type="text"
name="description"
/>
</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"
aria-hidden="false"
>
<h1 class="govuk-heading-l">
What is the name of the event?
</h1>
</label>
<div
id="event-name-hint"
class="govuk-hint"
aria-hidden="false"
>
The name you’ll use on promotional material.
</div>
<p
id="event-name-error"
class="govuk-error-message"
aria-hidden="false"
>
<span class="govuk-visually-hidden">Error:</span>
Enter an event name
</p>
<input
aria-describedby="event-name-hint event-name-error"
id="event-name-input"
class="govuk-input govuk-input--error"
type="text"
name="event-name"
/>
</div>
<TextInput
error="Enter an event name"
hint="The name you’ll use on promotional material."
label={
<h1 className="govuk-heading-l">
What is the name of the event?
</h1>
}
name="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"
aria-hidden="false"
>
<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"
aria-hidden="false"
>
<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
aria-describedby="cost-per-item-error"
id="cost-per-item-input"
class="govuk-input govuk-input--error"
style="max-width:10.81ex"
type="text"
name="cost-per-item"
/>
<div
class="govuk-input__suffix"
aria-hidden="true"
>
per item
</div>
</div>
</div>
<TextInput
error="Enter a cost per item, in pounds"
label={
<h1 className="govuk-heading-l">
What is the cost per item, in pounds?
</h1>
}
name="cost-per-item"
prefix="£"
suffix="per item"
width={5}
/>
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’.