Skip to content
shadcn.io

shadcn.io is not affiliated with official shadcn/ui

Input examples · Validation

Error State Input for React

An invalid email field with an error message under it.

Framework
UI library

An error should say what's wrong, right where it happened. This example marks the field invalid, which turns the input's border and ring destructive, and shows Please enter a valid email address under it in FieldError, next to a CircleAlertIcon. Use it to show validation errors after submit or on blur.

Preview

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

FAQ

How is the field marked invalid?

invalid on Field. It sets aria-invalid on the input, which the input's styles turn into a destructive border and ring, and screen readers announce the field as invalid.

How is the message shown?

In FieldError after the input, in destructive text with a CircleAlertIcon at size-4. The icon has aria-hidden, since the text says it all.

When does the error show?

In this example, always: the field is marked invalid with invalid-email as its default value. In a form, set invalid from your validation, after submit or when the field loses focus, and render the message only then.

Is the error announced?

FieldError is a live region, so screen readers announce the message when it appears.

How do I use it with a form library?

Set invalid on Field from the library's field state and put its message in FieldError. The input and the message then follow your validation.

How is Email tied to the field?

Clicking the label focuses the field, and screen readers announce it with the field. FieldLabel sets htmlFor to error-email, the input's id.