PasswordStrengthInfo
A strength meter with a checklist of rules. Pass the current password. The built-in rules are length (minLength, 8 by default), an uppercase letter, a lowercase letter, a number and a special character; the strength is how many are met, shown as a label (labels renames them, colors recolors the bar) and as a role="meter". showStrength and showRequirements hide either part.
- At least 8 charactersnot met
- An uppercase letternot met
- A lowercase letternot met
- A numbernot met
- A special characternot met
Your own rules. rules replaces the built-in set and extraRules adds to it. A rule is { label, test(password), required? }: required: false makes it a recommendation that counts towards the strength but never blocks. passwordRules has ready-made ones (minLength(n), uppercase, lowercase, digit, special, noSpaces, notContaining(words)), and defaultPasswordRules(minLength) is the built-in set. onResult reports { score, total, level, label, valid, met } whenever it changes, so a form can enable its submit button when valid (every required rule is met).
- At least 10 charactersnot met
- A numbernot met
- No spacesmet
- Not "password" or "banana"met
- A symbol such as ! or ?(recommended)not met
Only while typing. showOn="focus" hides the meter until the password field has focus (keyboard users get it on Tab), which keeps a sign-up form short. By default focus anywhere inside the parent element of the info counts, so wrap the input and the info together; or point focusTarget at the input (its id, a ref or the element). onResult keeps reporting while it is hidden, so a submit button still follows valid.
- At least 8 charactersnot met
- An uppercase letternot met
- A lowercase letternot met
- A numbernot met
- A special characternot met
Props
PasswordStrengthInfo
passwordstringCurrent password to evaluate.
rulesPasswordRule[]Your own rules, replacing the built-in ones. See the PasswordRule table below.
extraRulesPasswordRule[]Rules added after the built-in ones (ignored when
rulesis set). See the PasswordRule table below.minLengthnumberDefault:8Minimum length for the built-in length rule.
showStrengthbooleanDefault:trueShow the strength label and bar.
showRequirementsbooleanDefault:trueShow the requirement checklist.
labelsstring[]Default:['Very weak', 'Weak', 'Fair', 'Good', 'Strong']Strength names, weakest first.
colorsstring[]Default:red to emeraldTailwind background classes for the bar, one per label.
emptyLabelstringDefault:'Enter a password'Shown while the password is empty.
onResult(result) => voidCalled when the outcome changes with { score, total, level, label, valid, met }. It keeps reporting while the info is hidden.
showOn'always' | 'focus'Default:'always'Show it all the time, or only while the password field has focus.
focusTargetHTMLElement | RefObject | stringDefault:its parent elementWith
showOn="focus": the element (or ref, or element id) that has to hold focus for the info to show.classNamestringDefault:''Extra classes for the root element.
| Prop | Type | Default | Description |
|---|---|---|---|
password | string | – | Current password to evaluate. |
rules | PasswordRule[] | – | Your own rules, replacing the built-in ones. See the PasswordRule table below. |
extraRules | PasswordRule[] | – | Rules added after the built-in ones (ignored when rules is set). See the PasswordRule table below. |
minLength | number | 8 | Minimum length for the built-in length rule. |
showStrength | boolean | true | Show the strength label and bar. |
showRequirements | boolean | true | Show the requirement checklist. |
labels | string[] | ['Very weak', 'Weak', 'Fair', 'Good', 'Strong'] | Strength names, weakest first. |
colors | string[] | red to emerald | Tailwind background classes for the bar, one per label. |
emptyLabel | string | 'Enter a password' | Shown while the password is empty. |
onResult | (result) => void | – | Called when the outcome changes with { score, total, level, label, valid, met }. It keeps reporting while the info is hidden. |
showOn | 'always' | 'focus' | 'always' | Show it all the time, or only while the password field has focus. |
focusTarget | HTMLElement | RefObject | string | its parent element | With showOn="focus": the element (or ref, or element id) that has to hold focus for the info to show. |
className | string | '' | Extra classes for the root element. |
PasswordRule
keystringIdentifies the rule (default: its position).
labelReactNodeWhat the password must do, e.g. "At least 8 characters".
test(password) => booleantrue when the password satisfies the rule.
requiredbooleanDefault:truefalse makes it a recommendation: it counts towards the strength, but a failure does not make valid false.
| Prop | Type | Default | Description |
|---|---|---|---|
key | string | – | Identifies the rule (default: its position). |
label | ReactNode | – | What the password must do, e.g. "At least 8 characters". |
test | (password) => boolean | – | true when the password satisfies the rule. |
required | boolean | true | false makes it a recommendation: it counts towards the strength, but a failure does not make valid false. |