Skip to content

PasswordStrength ​

A standalone password-strength meter that does not wrap the input: associate it via for or auto-discover a password input inside the default slot — it composes with oas-input as well as native inputs. Built-in 0–4 scoring (length tiers + character-class diversity incl. CJK − common weak-password / repetition / sequential-run penalties); controlled level, levels label override, and a scoreFn custom scorer property; a 4-segment bar fully themed with tokens (dark mode automatic) with a default width of 12rem (--oas-password-strength-width overrides); the part="bar" segment bar carries the role="meter" accessibility semantics (the host stays generic so a slotted input is not flattened by the meter); level labels are localized in 16 languages.

Basic Usage (for + oas-input) ​

for association + show-label

for points to the id of a password input (resolved within the same root node): the target can be oas-input type="password" or a native <input type="password">. The meter listens to the input's input event and re-scores in real time, lighting up segments and updating the level label.

Try 123456 (exact weak-list match, always the weakest level) or password (weak-list body → 0). password1! with extras is no longer zeroed — it scores by length/classes; Abc!23456 (sequential-digit run penalty); MyStr0ng!Pass (long and diverse, full meter).

Level Label (show-label) ​

Default (hidden) vs show-label (visible)

By default only the segment bar renders (pure visual feedback); with show-label the current level label shows at the end of the bar (defaults to the i18n labels, tinted by level).

Controlled Level (level) ​

level=0–4, host-owned (auto-scoring paused)

level=0level=1level=2level=3level=4

The level attribute (0–4) switches to controlled mode: the displayed value is entirely host-owned — the component stops auto-scoring, the associated input no longer affects the meter, and oas-score-change is not dispatched (no two-way binding loops). Out-of-range values clamp to 0–4; invalid values fall back to 0 with a one-time console.warn. Removing level resumes auto-scoring.

Use this mode to bind your own scoring system (e.g. a strength field returned by the server): the meter is a pure presentation layer.

Custom Scoring (scoreFn) ​

Length-only custom scorer

Length only (≥12 chars = Very strong, 8–11 = Strong, otherwise Weak)

scoreFn is a JS property (bind with :scoreFn in Vue templates): it receives the password string and returns 0–4. While set it fully replaces the built-in scorer; set it to null to restore the built-in one. If the function throws, the meter falls back to the built-in score with a one-time console.warn. It is ignored while controlled level is present (host-owned value wins).

Slot Auto-Discovery ​

Native input inside the slot (no for)

Without for, the component auto-discovers the first [type=password] input inside the default slot (a native input, or a hosting component such as oas-input that declares type="password"), and binds the listener automatically. Slot content changes (slotchange) and removal of the associated target (a root-node childList observer) re-bind automatically: when the target is removed the score falls back to the empty state, with no dangling listeners and no errors.

When both for and a slot input are available, the for association wins.

Built-in Scoring Rules ​

ScoreSegments litVisual levelDefault label (en)
00neutral (empty)Very weak
11dangerWeak
22warningFair
33successStrong
44successVery strong

Scoring (0–4, original implementation):

  • Length tiers (effective length = 1 per ASCII char, 3 per CJK char): ≥8 → +1, ≥12 → +1, ≥20 → +1
  • Character-class diversity: lowercase / uppercase / digits / symbols / CJK — ≥2 classes → +1, ≥3 classes → +1 more (positive score capped at 4)
  • Common weak-password penalty: a built-in set of about 50 curated high-frequency weak passwords (123456 / password / qwerty / letmein …) — an exact lowercase match scores 0 (password → 0); substring matches are NOT zeroed — a strong password containing a weak word (Str0ng!Passw0rd contains passw0rd) keeps its entropy-derived score
  • Repetition penalty: ≥5 identical characters (e.g. aaaaaa) → capped at 1
  • Sequential-run penalty: a ≥5-char ascending/descending run (e.g. 12345 / abcde) → capped at 2

Labels and Localization ​

Level labels default to i18n (passwordStrength.0–passwordStrength.4, all 16 languages) and follow config-provider locale switches. Pass a JSON array to levels (5 non-empty strings) to override the whole set: levels='["Too weak","Weak","Fair","Strong","Very strong"]'; invalid JSON or a length other than 5 falls back to the i18n defaults with a one-time console.warn.

levels label override

Accessibility ​

  • The segment bar (part="bar") carries role="meter" + aria-valuenow / aria-valuemin="0" / aria-valuemax="4", with the current level label in aria-label — screen readers perceive both the score and its meaning
  • The host stays a plain container without an indicator role: a password input slotted inside is a flat-tree sibling of the bar, so it is not swallowed by the meter role's "children are presentational" semantics (the nested-interactive regression is pinned by the qa-regression spec); a pure indicator — no focus grab, not in the tab order
  • Segments and label encode the level twice (color + text), so color-blind users can still tell levels apart; the label uses -text safe-contrast tokens (≥4.5:1 on light backgrounds) and the segment fills use solid tokens (≥3:1 for non-text graphics)

Known Limitations ​

  • When the target input carries an initial value, the component scores it on mount and fires oas-score-change once on the first frame (a natural consequence of "fire on change"). Hosts that attach addEventListener after appendChild should read the level attribute for the initial score instead of relying on that first event.

API ​

oas-password-strength ​

Attributes ​

AttributeDescriptionTypeDefault
forID of the password input to associate with (resolved within the same root node; can be a native input[type=password] or a component hosting one, e.g. oas-input); takes precedence over slot auto-discoverystring—
levelControlled score (0–4): stops auto-scoring while present and dispatches no events (the value is host-owned); removing it resumes auto mode; out-of-range values clamp to 0–4, invalid values fall back to 0 with a one-time warningstring—
levelsFive-level label override: JSON array (5 non-empty strings, indexes 0–4); defaults to locale passwordStrength.*; invalid values fall back with a one-time warningstring—
show-labelShow the current level labelboolean—

Property (JS property; see each row for attribute reflection) ​

PropertyDescriptionTypeDefault
scoreFnCustom scoring function (password: string) => 0–4: fully replaces the built-in scorer while set; set null to restore. Ignored while controlled level is presentPasswordScoreFn | null—

Events ​

EventDescription
oas-score-changeFires when the score changes (auto/custom scoring modes only; not in controlled level mode), detail: { score, label }

Slots ​

NameDescription
defaultAuto-discovery area: the first [type=password] input (native input or hosting component) is discovered here and bound automatically

CSS Variables ​

CSS VariableDefault
--oas-password-strength-width12rem

Colors: the bar and label use only --oas-color-* tokens (--oas-color-danger / --oas-color-warning / --oas-color-success / --oas-color-bg-hover / --oas-color-text-*); dark and high-contrast themes work automatically. Style parts part="bar" / part="segment" / part="label" can be customized via host ::part().

Released under the MIT OR Apache-2.0 License.