Ask users for Dates
Overview
Use this pattern to help users enter a date.
When to use this pattern
Use the date pattern when you need to ask users for a date they will already know. For example, their date of birth.
When not to use this pattern
Do not use the date pattern if you are asking the user about an event they are unlikely to know the exact date of.
How to use this pattern
To allow the user to enter a day, month and year, this pattern uses three input components.
The 3 input fields are grouped within a fieldset.
A <legend> that lets the user know what they need to enter is added to this fieldset.
A description is used to provide an example of the required format for the date: “For example, 31 3 1980”. Do not use acronyms such as ‘e.g.’ in this description.
Never automatically tab the user between the fields. This can be confusing and may hinder normal keyboard control.
The date fields have a maxlength
limit set to the maximum number of digits required for day and month (2), and year (4), to prevent users entering too many digits in a single field.
This prevents users who expect to be able to auto-tab between the fields from entering the full date into the first field.
Each field uses the width: <number>
parameter to set the number of digits required for each field.
Date of birth
Use the autocomplete
attribute when you are asking for a date of birth to help the user answer the question faster.
To do this, set the autocomplete
attribute on each of the 3 date input fields to:
bday-day
bday-month
bday-year
See how to do this in the HTML and Nunjucks code tabs in the examples above.
You will need to use the autocomplete attribute to meet AA WCAG 2.1 accessibility requirements.
How to check dates
To help users enter a valid date, you should:
- check they have entered something in the date fields
- check that what they have entered is valid
- show an error message if they have not entered anything or what they have entered is not valid
Error messages
Show the error details above the date fields and use the correct errors pattern.
If all fields are empty
Use “Enter [whatever date is being asked for]”.
For example, “Enter your date of birth”.
If one or two of the fields are empty
Use “Enter the [whatever part or parts of the date is missing]”.
For example, “Enter the year you were born” or “Enter the month and year you were born”.
If what is entered in one of the fields is not a number
Use “Enter a number for [invalid field], for example, [appropriate number]”.
For example, “Enter a number for months, for example, 7”.
If what is entered in a field is above the maximum value for that field
Use “Enter a number that is less than [whatever the maximum is for that part of the date]”.
For example, “Enter a number that is less than 12” for months.
If what is entered is not within the required date range
Use “Enter a date that is between [whatever the minimum is] and [whatever the maximum is]”.
For example, “Enter a date that is between 1 April 2019 and 31 March 2020”.
If the date range entered is not valid
Use “Enter a date that is after [whatever the start date is]”.
For example, “Enter a date that is after 1 January 1999”.
Help improve this page
Let us know how we could improve this page, or share your user research findings. Discuss the ‘Dates’ pattern on GitHub (opens in a new tab)