Components
Validation
Stating validation requirements up front, with live feedback, means users won’t be left guessing.
Guidance
When to use the validation component
Provide helpful validation feedback. Use the validation component to give immediate feedback on which validation requirements have been successfully met, and which are not completed yet.
Using the validation component
- Validate on the server side as well. The validation component is intended primarily for usability, not as a robust security solution, since all the validation logic occurs on the client side. The validation should be “mirrored” on the server-side for security purposes.
- Connect the input element to your validation checklist. Set the value of your input’s
data-validation-element
to theid
of its related.usa-checklist
. For example, if the.usa-checklist
element has anid
ofvalidate-code
, usedata-validation-element="validate-code"
. - Customize the validation attributes. For each type of validation you’d like to provide feedback on:
- Come up with a custom name for your validator, e.g.
uppercase
. This name shouldn’t have any spaces in it. - On the appropriate list item in
.usa-checklist
, set thedata-validator
attribute to the name of your custom validator, e.g.data-validator="uppercase"
. This is the list item that will appear “checked” when the validator’s condition is met. - On the input element, add an attribute called
data-validate-[validator name]
, where[validator name]
is the custom name you created in step 1. Define this attribute with a regular expression (described on RegexOne) that represents whether the validator’s condition is met. For example, if your custom validator is calleduppercase
, usedata-validate-uppercase="[A-Z]"
.
For reference, you can find the complete implementation of this configuration in the “Component code” accordion panel at the top of this page.
- Come up with a custom name for your validator, e.g.
Validation settings
This component has no settings.
Validation variants
This component has no variants.
Package
-
Package usage:
@forward "usa-validation";
- Dependencies:
usa-form
,usa-fieldset
,usa-legend
,usa-alert
,usa-checklist
,usa-label
,usa-input
,usa-button
Latest updates
Meaningful code and guidance updates are listed in the following table:
Date | USWDS version | Affects | Description |
---|---|---|---|
2022-12-21 | N/A |
|
Clarified implementation guidance and removed the |
2022-10-19 | 3.2.0 |
|
Improved status updates about the completion of validation requirements for users of assistive technologies. Component JavaScript generates the appropriate elements and ARIA attributes that help improve the experience of receiving status updates. More information: uswds#4914 |
2022-04-28 | N/A |
|
Updated package name to |
2022-04-28 | 3.0.0 |
|
Breaking Updated to Sass module syntax and new package structure. More information: uswds#4656 |
2021-06-07 | N/A |
|
Updated package name to |