Date input
<gcds-date-input>
Also called: dates, dateinput, date, memorable date.
A date input is a space to enter a known date.
-
GitHub -
Figma
Date input component preview
Build a date input
Use a date input to collect a date from a person when you’re expecting them to enter a date they already know or can verify.
Coding and accessibility for date inputs
Apply required attributes
For the date input to function properly, always use the following attributes with <gcds-date-input>
:
name
legend
format
Format the date input
- Choose the format of the date input by entering either
full
orcompact
in theformat
attribute of the date input. - The
full
value will render the date input with a year, month, and day form field in the order month, day, and year in English and the order of day, month, and year in French. - The
compact
value will render the date input with a year and month form field in the order of month and year in both English and French. - The
format
attribute also formats the value received and outputted by the date input. The value will be formattedYYYY-MM-DD
while set asfull
andYYYY-MM
while set ascompact
.
Entering and receiving the value from date input
Date input can receive and output different value formats depending on the format
attribute:
- The
full
format will expect/output a value formattedYYYY-MM-DD
. - The
compact
format will expect/output a value formattedYYYY-MM
.
Use valid properties
Component properties have built-in validation. Required properties or properties prone to misuse will be validated. If there are any validation errors, the component will be non-functional and won't render. Review the browser's console for details and adjust the properties accordingly to ensure proper functionality.
Handle error messages and validation
-
Use the
required
attribute to activate the required validator. Validation will happen by default on theonBlur
event. A missing or invalid entry will prompt an inline error message with preset text. -
If you need to change the validation event, use the
validate-on
attribute. Validation can happen on blur, when the element loses focus, or manually with thevalidate()
method. -
Use the
required
attribute for fields that must be filled in. This places "required" at the end of the label. -
Use the
error-message
attribute to include an error message text for all required inputs. Avoid using error messages for optional ones. -
For optional fields, avoid adding the
error-message
attribute. -
For an optional field that needs validation based on user input (like validating an email address format), opt to add custom validation with a custom error message.
Include a hint text for task success
-
Use the
hint
attribute to add hint text to help a person provide a complete value in the input and avoid an error state. -
Avoid adding hint text in the field (sometimes called placeholder text) where it will disappear once the field is selected or a response starts to be entered.
Get your code
Generate an instance of the component you need by selecting its code properties.
-
Explore by choosing different code values to generate the instance you want.
-
Get the code and pull it into your environment.
-
Add any copy you need to the component (like text for a label).
Note: The code builder uses English for all code elements, which follows standard practice.
Help us improve
Have questions or a request? Give feedback on our contact form.
Something's wrong? Raise it through GitHub with an