AniUI Academy

Build a password strength meter

easyAccessibility+160 XP

Build a password input that scores strength live as the user types.

Score is based on four signals: length ≥ 8, contains a lowercase letter, contains an uppercase letter, contains a digit or symbol. Each adds 1 to the score (0–4).

The label updates: 0 → Empty, 1 → Weak, 2 → Fair, 3 → Good, 4 → Strong. The meter bar fills proportionally.

Examples

// password = ""         → score 0, label "Empty"
// password = "aaaaaaaa"  → score 2 (length + lowercase), "Fair"
// password = "aaaaaaaA"  → score 3 (+ uppercase),        "Good"
// password = "aaaaaaA1"  → score 4 (+ digit),            "Strong"

// bar.aria-valuenow mirrors the score every time.

One point for each of: length ≥ 8, has lowercase, has uppercase, has digit/symbol.

function scoreOf(password) {
  if (!password) return 0;
  let s = 0;
  if (password.length >= 8) s++;
  if (/[a-z]/.test(password)) s++;
  if (/[A-Z]/.test(password)) s++;
  if (/[\d\W]/.test(password)) s++;
  return s;
}

const score = scoreOf(password);
const label = ["Empty","Weak","Fair","Good","Strong"][score];

Derive both score and label during render — no state needed for either.

What it has to do

  • The input is `<input type="password" data-testid="password" />`.
  • The score label is `<p data-testid="strength-label">`, with text one of: Empty, Weak, Fair, Good, Strong.
  • The score bar is `<div data-testid="strength-bar" aria-valuenow="N" aria-valuemin="0" aria-valuemax="4" role="progressbar" />` with aria-valuenow equal to the current score (0–4).
  • An empty password renders label "Empty" and aria-valuenow="0".
  • A `<p data-testid="strength-announce" role="status" aria-live="polite">` announces the current label, so a screen reader hears strength changes without the user focusing the meter.

Notes

  • Rule-based meters are notoriously bad at reflecting real crackability; a production meter should use `zxcvbn` (entropy-based) rather than character-class counts.
  • Never log the actual password, even for a dev-mode strength breakdown — show which rules passed/failed instead.
  • `role="progressbar"` + `aria-valuemin`/`aria-valuemax`/`aria-valuenow` is the standard pattern; sighted users rely on the bar, SR users rely on the attributes.
  • The `aria-live="polite"` announcer should mirror the label, not the raw score — screen readers read "Strong" more usefully than "4".
Asked atMicrosoftGoogleStripeRazorpay

Stuck? The react course covers the ideas this problem is built on.

import { useState } from "react";

const LABELS = ["Empty", "Weak", "Fair", "Good", "Strong"];

export default function App() {
  const [password, setPassword] = useState("");

  // TODO: compute score (0..4) from password; look up the label.
  const score = 0;
  const label = LABELS[score];

  return (
    <div style={{ fontFamily: "sans-serif", padding: 16, maxWidth: 320 }}>
      <input
        type="password"
        data-testid="password"
        value={password}
        onChange={(e) => setPassword(e.target.value)}
        style={{ width: "100%", padding: 8 }}
      />
      <div
        data-testid="strength-bar"
        role="progressbar"
        aria-valuenow={score}
        aria-valuemin={0}
        aria-valuemax={4}
        style={{ height: 8, background: "#eee", borderRadius: 4, marginTop: 8 }}
      >
        <div style={{ height: "100%", width: `${(score / 4) * 100}%`, background: "#4caf50" }} />
      </div>
      <p data-testid="strength-label" style={{ margin: 4 }}>{label}</p>
      <p data-testid="strength-announce" role="status" aria-live="polite" style={{ position: "absolute", left: -9999 }}>
        {label}
      </p>
    </div>
  );
}

5 tests · +160 XP

Discussion

Sign in to post
Sign in to join the conversation.Sign in

No comments yet. Be the first to say something.