Date input
Use the date input component to help users enter a memorable date or one they
can easily look up.
<div id="passport-issued" class="govuk-form-group">
<fieldset
aria-describedby="passport-issued-hint"
class="govuk-fieldset"
>
<legend class="govuk-fieldset__legend">
<h1 class="govuk-heading-l">
When was your passport issued?
</h1>
</legend>
<div
id="passport-issued-hint"
class="govuk-hint"
aria-hidden="false"
>
For example, 12 11 2007
</div>
<div class="govuk-date-input">
<div class="govuk-date-input__item">
<label
for="passport-issued-day"
class="govuk-label"
aria-hidden="false"
>
Day
</label>
<input
id="passport-issued-day"
inputmode="numeric"
class="govuk-input govuk-input--width-2 govuk-date-input__input"
type="text"
name="passport-issued[day]"
/>
</div>
<div class="govuk-date-input__item">
<label
for="passport-issued-month"
class="govuk-label"
aria-hidden="false"
>
Month
</label>
<input
id="passport-issued-month"
inputmode="numeric"
class="govuk-input govuk-input--width-2 govuk-date-input__input"
type="text"
name="passport-issued[month]"
/>
</div>
<div class="govuk-date-input__item">
<label
for="passport-issued-year"
class="govuk-label"
aria-hidden="false"
>
Year
</label>
<input
id="passport-issued-year"
inputmode="numeric"
class="govuk-input govuk-input--width-4 govuk-date-input__input"
type="text"
name="passport-issued[year]"
/>
</div>
</div>
</fieldset>
</div>
<DateInput
hint="For example, 12 11 2007"
label={
<h1 className="govuk-heading-l">
When was your passport issued?
</h1>
}
name="passport-issued"
/>
Props
|
| string | - | 'id' attribute to place on the base HTML element
HTML id (If not specified then the name will be used) |
| 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 |
| DateInputValue | - | Initial value of the field |
| DateInputError | - | Error message |
| ReactNode | For example, 12 11 2007 | Hint |
| ReactNode | - | REQUIRED. Label |
| DateInputValue | - | Value for controlled fields |
When to use this component
Use the date input component when you’re asking users for a date they’ll already
know, or can look up without using a calendar.
When not to use this component
Do not use the date input component if users are unlikely to know the exact date
of the event you’re asking about.
Read more about how to ask users for dates.
How it works
The date input component consists of 3 fields to let users enter a day, month
and year.
The 3 date fields are grouped together in a <fieldset> with a <legend> that
describes them, as shown in the examples on this page. This is usually a
question, like ‘What is your date of birth?’.
If you’re asking one question per page, you can set the contents of the
<legend> 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="passport-issued" class="govuk-form-group">
<fieldset
aria-describedby="passport-issued-hint"
class="govuk-fieldset"
>
<legend class="govuk-fieldset__legend">
<h1 class="govuk-heading-l">
When was your passport issued?
</h1>
</legend>
<div
id="passport-issued-hint"
class="govuk-hint"
aria-hidden="false"
>
For example, 12 11 2007
</div>
<div class="govuk-date-input">
<div class="govuk-date-input__item">
<label
for="passport-issued-day"
class="govuk-label"
aria-hidden="false"
>
Day
</label>
<input
id="passport-issued-day"
inputmode="numeric"
class="govuk-input govuk-input--width-2 govuk-date-input__input"
type="text"
name="passport-issued[day]"
/>
</div>
<div class="govuk-date-input__item">
<label
for="passport-issued-month"
class="govuk-label"
aria-hidden="false"
>
Month
</label>
<input
id="passport-issued-month"
inputmode="numeric"
class="govuk-input govuk-input--width-2 govuk-date-input__input"
type="text"
name="passport-issued[month]"
/>
</div>
<div class="govuk-date-input__item">
<label
for="passport-issued-year"
class="govuk-label"
aria-hidden="false"
>
Year
</label>
<input
id="passport-issued-year"
inputmode="numeric"
class="govuk-input govuk-input--width-4 govuk-date-input__input"
type="text"
name="passport-issued[year]"
/>
</div>
</div>
</fieldset>
</div>
<DateInput
hint="For example, 12 11 2007"
label={
<h1 className="govuk-heading-l">
When was your passport issued?
</h1>
}
name="passport-issued"
/>
Never automatically tab users between the fields of the date input because this
can be confusing and may clash with normal keyboard controls.
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="passport-issued" class="govuk-form-group">
<fieldset
aria-describedby="passport-issued-hint"
class="govuk-fieldset"
>
<legend class="govuk-fieldset__legend">
When was your passport issued?
</legend>
<div
id="passport-issued-hint"
class="govuk-hint"
aria-hidden="false"
>
For example, 12 11 2007
</div>
<div class="govuk-date-input">
<div class="govuk-date-input__item">
<label
for="passport-issued-day"
class="govuk-label"
aria-hidden="false"
>
Day
</label>
<input
id="passport-issued-day"
inputmode="numeric"
class="govuk-input govuk-input--width-2 govuk-date-input__input"
type="text"
name="passport-issued[day]"
/>
</div>
<div class="govuk-date-input__item">
<label
for="passport-issued-month"
class="govuk-label"
aria-hidden="false"
>
Month
</label>
<input
id="passport-issued-month"
inputmode="numeric"
class="govuk-input govuk-input--width-2 govuk-date-input__input"
type="text"
name="passport-issued[month]"
/>
</div>
<div class="govuk-date-input__item">
<label
for="passport-issued-year"
class="govuk-label"
aria-hidden="false"
>
Year
</label>
<input
id="passport-issued-year"
inputmode="numeric"
class="govuk-input govuk-input--width-4 govuk-date-input__input"
type="text"
name="passport-issued[year]"
/>
</div>
</div>
</fieldset>
</div>
<DateInput
hint="For example, 12 11 2007"
label="When was your passport issued?"
name="passport-issued"
/>
Use the autocomplete attribute for a date of birth
Use the autocomplete attribute on the date input component when you’re asking
for a date of birth. This lets browsers autofill the information on a user’s
behalf if they’ve entered it previously.
To do this, set the autocomplete prop to bday. See how to do this in the code
snippet in the following example.
<div id="dob" class="govuk-form-group">
<fieldset
aria-describedby="dob-hint"
class="govuk-fieldset"
>
<legend class="govuk-fieldset__legend">
<h1 class="govuk-heading-l">
What is your date of birth?
</h1>
</legend>
<div
id="dob-hint"
class="govuk-hint"
aria-hidden="false"
>
For example, 31 3 1980
</div>
<div class="govuk-date-input">
<div class="govuk-date-input__item">
<label
for="dob-day"
class="govuk-label"
aria-hidden="false"
>
Day
</label>
<input
id="dob-day"
inputmode="numeric"
autocomplete="bday-day"
class="govuk-input govuk-input--width-2 govuk-date-input__input"
type="text"
name="dob[day]"
/>
</div>
<div class="govuk-date-input__item">
<label
for="dob-month"
class="govuk-label"
aria-hidden="false"
>
Month
</label>
<input
id="dob-month"
inputmode="numeric"
autocomplete="bday-month"
class="govuk-input govuk-input--width-2 govuk-date-input__input"
type="text"
name="dob[month]"
/>
</div>
<div class="govuk-date-input__item">
<label
for="dob-year"
class="govuk-label"
aria-hidden="false"
>
Year
</label>
<input
id="dob-year"
inputmode="numeric"
autocomplete="bday-year"
class="govuk-input govuk-input--width-4 govuk-date-input__input"
type="text"
name="dob[year]"
/>
</div>
</div>
</fieldset>
</div>
<DateInput
autoComplete="bday"
hint="For example, 31 3 1980"
label={
<h1 className="govuk-heading-l">
What is your date of birth?
</h1>
}
name="dob"
/>
If you are working in production you’ll need to do this 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.
Error messages
Error messages should be styled like this:
<div
id="passport-issued"
class="govuk-form-group govuk-form-group--error"
>
<fieldset
aria-describedby="passport-issued-hint passport-issued-error"
class="govuk-fieldset"
>
<legend class="govuk-fieldset__legend">
<h1 class="govuk-heading-l">
When was your passport issued?
</h1>
</legend>
<div
id="passport-issued-hint"
class="govuk-hint"
aria-hidden="false"
>
For example, 12 11 2007
</div>
<p
id="passport-issued-error"
class="govuk-error-message"
aria-hidden="false"
>
<span class="govuk-visually-hidden">
Error:
</span>
The date your passport was issued must be in the
past
</p>
<div class="govuk-date-input">
<div class="govuk-date-input__item">
<label
for="passport-issued-day"
class="govuk-label"
aria-hidden="false"
>
Day
</label>
<input
id="passport-issued-day"
inputmode="numeric"
class="govuk-input govuk-input--width-2 govuk-date-input__input"
type="text"
name="passport-issued[day]"
/>
</div>
<div class="govuk-date-input__item">
<label
for="passport-issued-month"
class="govuk-label"
aria-hidden="false"
>
Month
</label>
<input
id="passport-issued-month"
inputmode="numeric"
class="govuk-input govuk-input--width-2 govuk-date-input__input"
type="text"
name="passport-issued[month]"
/>
</div>
<div class="govuk-date-input__item">
<label
for="passport-issued-year"
class="govuk-label"
aria-hidden="false"
>
Year
</label>
<input
id="passport-issued-year"
inputmode="numeric"
class="govuk-input govuk-input--width-4 govuk-date-input__input"
type="text"
name="passport-issued[year]"
/>
</div>
</div>
</fieldset>
</div>
<DateInput
error="The date your passport was issued must be in the past"
hint="For example, 12 11 2007"
label={
<h1 className="govuk-heading-l">
When was your passport issued?
</h1>
}
name="passport-issued"
/>
If you’re highlighting just one field - either the day, month or year - only style the field that has an error. The error message must say which field has an error, like this:
<div
id="passport-issued"
class="govuk-form-group govuk-form-group--error"
>
<fieldset
aria-describedby="passport-issued-hint passport-issued-error"
class="govuk-fieldset"
>
<legend class="govuk-fieldset__legend">
<h1 class="govuk-heading-l">
When was your passport issued?
</h1>
</legend>
<div
id="passport-issued-hint"
class="govuk-hint"
aria-hidden="false"
>
For example, 12 11 2007
</div>
<p
id="passport-issued-error"
class="govuk-error-message"
aria-hidden="false"
>
<span class="govuk-visually-hidden">
Error:
</span>
The date your passport was issued must include a
year
</p>
<div class="govuk-date-input">
<div class="govuk-date-input__item">
<label
for="passport-issued-day"
class="govuk-label"
aria-hidden="false"
>
Day
</label>
<input
id="passport-issued-day"
inputmode="numeric"
class="govuk-input govuk-input--width-2 govuk-date-input__input"
type="text"
name="passport-issued[day]"
/>
</div>
<div class="govuk-date-input__item">
<label
for="passport-issued-month"
class="govuk-label"
aria-hidden="false"
>
Month
</label>
<input
id="passport-issued-month"
inputmode="numeric"
class="govuk-input govuk-input--width-2 govuk-date-input__input"
type="text"
name="passport-issued[month]"
/>
</div>
<div class="govuk-date-input__item">
<label
for="passport-issued-year"
class="govuk-label"
aria-hidden="false"
>
Year
</label>
<input
id="passport-issued-year"
inputmode="numeric"
class="govuk-input govuk-input--width-4 govuk-input--error govuk-date-input__input"
type="text"
name="passport-issued[year]"
/>
</div>
</div>
</fieldset>
</div>
<DateInput
error={{
year: "The date your passport was issued must include a year",
}}
hint="For example, 12 11 2007"
label={
<h1 className="govuk-heading-l">
When was your passport issued?
</h1>
}
name="passport-issued"
/>
Make sure errors follow the guidance in error message and have specific error
messages for specific error states.
If there’s more than one error, show the highest priority error message. In order of priority, show error messages about:
- missing or incomplete information
- information that cannot be correct (for example, the number ‘13’ in the month field)
- information that fails validation for another reason
If nothing is entered
Say ‘Enter [whatever it is]’.
For example, ‘Enter your date of birth’.
If an incomplete date is entered and you know what is missing
Say ‘[whatever it is] must include a [whatever is missing]’.
For example, ‘Date of birth must include a month’ or ‘Date of birth must include a day and month’.
If an incomplete date is entered and you do not know what is missing
Say ‘Enter [whatever it is] and include a day, month and year’.
For example, ‘Enter your date of birth and include a day, month and year’.
If the date entered is not a real one
Say ‘Enter a real [whatever it is]’.
For example, ‘Enter a real date of birth’.
If the date is in the future when it needs to be in the past
Say ‘[whatever it is] must be in the past’.
For example, ‘Date of birth must be in the past’.
If the date is in the future when it needs to be today or in the past
Say ‘[whatever it is] must be today or in the past’.
For example, ‘Date of birth must be today or in the past’.
If the date is in the past when it needs to be in the future
Say ‘[whatever it is] must be in the future’.
For example, ‘The date your course ends must be in the future’.
If the date is in the past when it needs to be today or in the future
Say ‘[whatever it is] must be today or in the future’.
For example, ‘The date your course ends must be today or in the future’.
If the date must be the same as or after another date
Say ‘[whatever it is] must be the same as or after [date and optional description]’.
For example, ‘The date your course ends must be the same as or after 1 September 2017 when you started the course’.
If the date must be after another date
Say ‘[whatever it is] must be after [date and optional description]’.
For example, ‘The day your course ends must be after 1 September 2017’.
If the date must be the same as or before another date
Say ‘[whatever it is] must be the same as or before [date and optional description]’.
For example, ‘The date of Gordon’s last exam must be the same as or before 31 August 2017 when they left school’.
If the date must be before another date
Say ‘[whatever it is] must be before [date and optional description]’.
For example, ‘The date of Gordon’s last exam must be the same as or before 31 August 2017’.
If the date must be between two dates
Say ‘[whatever it is] must be between [date] and [date and optional description]’.
For example, ‘The date your contract started must be between 1 September 2017 and 30 September 2017 when you were self-employed’.