# Password Strength

> A React password field with a four step strength meter and a live rule checklist. Announces the score politely and accepts your own scorer such as zxcvbn.

- Kind: Component (Forms)
- Docs: https://ui.minidev.pro/docs/password-strength
- Source (registry JSON with the full file): https://ui.minidev.pro/r/password-strength.json
- License: MIT. One file, yours to edit after install.

## When to use

The score is announced politely as it changes, rules are plain text for screen readers ("(met)" / "(not met)"), and nothing blocks typing. Swap in your own scorer (for example zxcvbn) with the `score` prop and your own policy with `rules`. Use it on sign-up and change-password forms; on sign-in use PasswordInput alone.

## Installation

### shadcn CLI (copies the source into your project)

```bash
npx shadcn@latest add @minidev/password-strength
```

Writes `components/ui/password-strength.tsx` plus the MiniDev files it is built from (password-input) and installs its npm dependencies. Older CLI versions: `npx shadcn@latest add https://ui.minidev.pro/r/password-strength.json`.

### npm package

```bash
npm i minidev-ui-kit lucide-react
```

Add the tokens to your global stylesheet once:

```css
@import "tailwindcss";
@import "minidev-ui-kit/styles.css";
@source "../node_modules/minidev-ui-kit";
```

Next.js also needs `transpilePackages: ["minidev-ui-kit"]` in next.config.

## Import

After the shadcn install:

```tsx
import { PasswordStrength } from "@/components/ui/password-strength"
```

From the npm package:

```tsx
import { PasswordStrength } from "minidev-ui-kit/ui/password-strength"
```

## Usage

```tsx
import { PasswordStrength } from "@/components/ui/password-strength"

export function Example() {
  return (
    <PasswordStrength />
  )
}
```

## Props

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| `value` | `string` | - | no | Controlled password. |
| `defaultValue` | `string` | `""` | no | Initial password when uncontrolled. |
| `onChange` | `(value: string) => void` | - | no | Fires with the password on every edit. |
| `rules` | `Rule[]` | `DEFAULT_RULES` | no | Rules shown in the checklist. |
| `score` | `(value: string) => number` | - | no | Override the built in 0 to 4 score. |
| `label` | `string` | `"Password"` | no | Visible label above the field. |
| `id` | `string` | - | no | Input id; also prefixes the meter and checklist ids. |
| `name` | `string` | - | no | Form field name. |
| `className` | `string` | - | no |  |

## Examples

### Controlled with confirm

Hold the value in state to drive a confirm field and keep the submit button disabled until the two match.

```tsx
function ConfirmDemo() {
  const [password, setPassword] = React.useState("Northwind-2026")
  const [confirm, setConfirm] = React.useState("")
  const mismatch = confirm.length > 0 && confirm !== password
  const ready = password.length >= 12 && confirm === password
  return (
    <form className="flex w-full max-w-sm flex-col gap-4" onSubmit={(e) => e.preventDefault()}>
      <PasswordStrength id="new-password" label="New password" value={password} onChange={setPassword} />
      <FormField id="confirm-password" label="Confirm password" error={mismatch ? "The two passwords do not match." : undefined}>
        <PasswordInput
          id="confirm-password"
          value={confirm}
          autoComplete="new-password"
          aria-invalid={mismatch ? true : undefined}
          onChange={(e) => setConfirm(e.target.value)}
        />
      </FormField>
      <Button type="submit" disabled={!ready} className="self-start">Update password</Button>
    </form>
  )
}
```

### Custom rules

rules replaces the checklist; each rule is a label and a test, and the built-in score counts how many pass.

```tsx
const POLICY = [
  { label: "At least 10 characters", test: (v: string) => v.length >= 10 },
  { label: "No spaces", test: (v: string) => v.length > 0 && !/\s/.test(v) },
  { label: "Does not contain your name", test: (v: string) => v.length > 0 && !/maya|chen/i.test(v) },
  { label: "A number or symbol", test: (v: string) => /[^A-Za-z]/.test(v) },
]

<PasswordStrength label="Password" rules={POLICY} defaultValue="northwind" />
```

### Custom scorer

score overrides the meter with your own 0 to 4 number, for example from zxcvbn; the checklist still follows the rules.

```tsx
<PasswordStrength
  label="Passphrase"
  defaultValue="correct horse battery"
  score={(v) => Math.min(4, Math.floor(v.trim().split(/\s+/).filter(Boolean).length))}
  rules={[
    { label: "Three or more words", test: (v) => v.trim().split(/\s+/).filter(Boolean).length >= 3 },
    { label: "At least 16 characters", test: (v) => v.length >= 16 },
  ]}
/>
```

## Accessibility

- The input's `aria-describedby` points at the strength label and the rules list, so both are read with the field.
- The strength label ("Weak", "Good"…) is `aria-live="polite"`, so changes are announced as you type.
- Each rule ends with a visually hidden "(met)" or "(not met)". The meter bars and icons are `aria-hidden`.
- The input uses `autoComplete="new-password"` so password managers offer a generated password.

## FAQ

### How do I use zxcvbn or my own scoring?

Pass `score`, a function from the password to 0 to 4: `score={(v) => zxcvbn(v).score}`. The result is rounded and clamped, and the `rules` checklist still shows.

### Does it block weak passwords?

No. Nothing stops typing or submitting. Run the same `rules` on submit, and check again on the server.

### How is the default score worked out?

It counts the rules met, takes one off below 8 characters and adds one at 16 or more, then clamps to 1 to 4. With the default rules, "Northwind2026!" meets all four and scores 4, "Strong".

## Rules for generating code

- Use semantic tokens only (bg, surface, sunken, border, fg, fg-muted, fg-subtle, ink, accent, success, warning, danger). Never raw hex or palette steps like blue-500.
- Shadows only from the set: shadow-xs, shadow-sm, shadow-raised, shadow-md, shadow-lg, shadow-overlay.
- Numbers use tabular-nums. Dark mode is the `dark` class on <html>; components never branch on theme.

## Dependencies

- npm: `lucide-react`
- Built from: [password-input](https://ui.minidev.pro/docs/password-input/md)

## Related

- [Password Input](https://ui.minidev.pro/docs/password-input/md): A password field with a show and hide toggle inside the input, an eye icon that swaps state and an accessible label. Supports sm, default and lg sizes in React.
- [Autocomplete](https://ui.minidev.pro/docs/autocomplete/md): Text input that filters string options as you type, using ARIA combobox and listbox roles. Works controlled or uncontrolled and lists up to eight matches.
- [Calendar Month](https://ui.minidev.pro/docs/calendar-month/md): Month calendar grid with weekday headers and clickable day cells that highlight the selected date. A simple Tailwind base for date views and schedulers.
- [Character Count](https://ui.minidev.pro/docs/character-count/md): Live React character counter that shows the current count against a limit and turns red when it goes over. Announces changes politely to screen readers.
- [Checkbox Group](https://ui.minidev.pro/docs/checkbox-group/md): Vertical wrapper that groups related checkboxes under role group with even spacing. Use it with Checkbox and labels for multi select settings.
- [Checkbox](https://ui.minidev.pro/docs/checkbox/md): Shadcn style checkbox on Base UI with an animated check, indeterminate state, invalid and disabled styles, and an enlarged hit area for touch.

---

All 557 components: https://ui.minidev.pro/llms.txt (full text: https://ui.minidev.pro/llms-full.txt).
