--- title: Validation seoTitle: Form validation states order: 12 summary: Validation states mark a field as valid or invalid and show a message under it, so the user sees what to fix. description: Show valid and invalid states on form fields with feedback messages, so users know exactly what to correct before submitting. related: [/ui/forms/elements, /ui/forms/helpers] sass-vars: form-validation --- import Example from '@components/Example.astro'; ## Validation states Validation states mark a field as valid or invalid, so the user sees which fields to correct and, for an invalid one, why. To apply the validation state to the form control, use the `.is-valid` and `.is-invalid` classes. For assistive tech, also set `aria-invalid="true"` on invalid controls.
## Validation feedback To provide users with additional information about the validation state, you can use the `.valid-feedback` and `.invalid-feedback` classes. Link the message with `aria-describedby` when you can.
Please provide a valid city.
You can also use the `.valid-feedback` to provide users with positive feedback.
Looks good!
## Subtle validation states For a quieter version, show a tick or a cross icon instead of the colored frame and the feedback message. To do this, use the `.is-valid-lite` and `.is-invalid-lite` classes. These variants drop the colored frame and the feedback message, so the state is left to a small icon. Keep `aria-invalid` on the field, and keep the reason in text somewhere the field points to - otherwise the error exists only as a symbol.
## Accessibility - Colour alone must not say that a field is wrong. Keep `aria-invalid="true"` on the field and a message in text. - Give the feedback element an `id` and point the field at it with `aria-describedby`, so the message is read with the field rather than after the form. - Say what to do, not only what failed. "Please provide a valid city" beats "Invalid". - On submit, move focus to the first field that failed. Otherwise a keyboard user has to hunt for it. - Validate when the user leaves a field, not on every keystroke. A message that appears and disappears while typing is announced over and over.