Skip to content
shadcn.io

shadcn.io is not affiliated with official shadcn/ui

Input examples · Validation

Real-time Validation Input for React

A password field with a checklist of rules that tick off as you type.

Framework
UI library

A checklist is friendlier than an error after the fact. This example lists four password rules under the field, at least 8 characters, a number, an uppercase letter and a special character, and each one turns green with a check as soon as the password meets it. Use it on sign-up and password-change forms.

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 are the rules checked?

Each rule is a test function in a static list at the top of the file: a length check and three regular expressions. On every render, each rule is tested against the password in state.

How does a rule change when it's met?

Its row switches from text-muted-foreground with an XIcon to text-success with a CircleCheckIcon. The color and the icon change together, so it doesn't rely on color alone.

Where does the password live?

In state, written on every keystroke, so the checklist updates as people type.

How is the checklist marked up?

As a list, one item per rule, so screen readers announce how many rules there are. The icons have aria-hidden.

How do I change the rules?

Edit the list: each entry has a text and a test that takes the password and returns true when the rule is met. Keep the same rules on the server.

How is Create Password tied to the field?

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