Skip to main content
Login

Sign in to access your account

Password - National Design System

A password field that checks the password rules as the user types, checks that a retyped password matches, and blocks the form submit until both pass

Overview

A password field is a form field with the nds-password class, for a user who picks a new password. Each rule the password must meet is a chip under the field. A chip turns green when the value passes its rule and red when it fails. A second field can check that the user typed the same password twice. The show and clear buttons come from the forms script.

Pick another component when:

  • the user signs in with a password they already have: a plain password field in Forms
  • the user types a one-time code: OTP Input

Markup

Preview
<div class="nds-form-container nds-password" data-required> <div class="nds-form-header"> <label for="new-password"> <span class="nds-label">New password</span> </label> </div> <div class="nds-form-control"> <i class="nds-icon nds-hgi-lock-password" aria-hidden="true"></i> <input type="password" id="new-password" name="new-password" class="nds-input" autocomplete="new-password" minlength="8" aria-describedby="new-password-rules"> <div class="nds-form-action"> <button class="nds-btn nds-subtle nds-clear nds-icon-only" hidden type="button" aria-label="Clear password" data-i18n-aria-label="password.clear_password"> <i class="nds-icon nds-hgi-cancel-01" aria-hidden="true"></i> </button> <button class="nds-btn nds-subtle nds-toggle-password nds-icon-only" type="button" aria-label="Show password" data-i18n-aria-label="form.show_password"> <i class="nds-icon nds-hgi-view-off" aria-hidden="true"></i> </button> </div> </div> <div class="nds-form-footer" data-feedback-target> <div class="nds-password-rules" id="new-password-rules"> <span class="nds-feedback nds-outline" data-permanent data-status="neutral" data-rule="length"> <span class="nds-feedback-icon"><i class="nds-icon" aria-hidden="true"></i></span> <span class="nds-feedback-message">At least 8 characters</span> </span> </div> <span class="nds-password-status" role="status" aria-live="polite"></span> </div> </div>

Behavior

Pattern Rules

data-rule-pattern on a chip holds a regular expression that the value must match. The chip can use any data-rule name, and it counts and blocks the submit like a built-in rule. The expression matches anywhere in the value, so anchor it with ^ and $ to test the whole value. The match is case-sensitive, and the chip takes no flags. For a “must not contain” rule, use a lookahead, such as ^(?!.*admin). An invalid expression logs a warning, and the chip stays neutral.

Confirm Match

data-password-match on the retype field’s container names the first input, as a CSS selector. The chip with data-rule="match" turns green when the two values are equal. It updates while the user types in either field. A mismatch turns the chip red and blocks the submit.

The chip is optional. Without it, leave out .nds-password-rules and the input’s aria-describedby, but keep the footer and the status region. The submit is still blocked, and the message “The two passwords do not match” shows at submit.

Built-in Features

  • Auto-initialization

    Every nds-password field on the page starts on its own. A value already in the field, such as one from the server, shows its rule status at once.

  • Five Built-in Rules

    Length, capital letter, small letter, number and symbol. A symbol is any character other than A-Z, a-z and 0-9. Add a chip for each rule you want.

  • Submit Gating

    A failing rule or a mismatch blocks the submit through native constraint validation. The field outline turns red, and the red chips name the cause, so no message is added. The chips stay neutral while the field is empty.

  • Show Password

    The eye button switches the field between hidden and plain text. Its label changes between "Show password" and "Hide password". The script hides the password again when the form is sent or reset.

  • Arabic Letters Blocked

    A password field does not accept Arabic letters. The forms script removes a typed Arabic letter at once and shows an error under the field.

  • Screen Reader Progress

    A hidden live region reads "3 of 5 password rules met", or the mismatch message, half a second after the user stops typing. The chips are not read again on each keystroke.

  • Strength Value in CSS

    The container carries the number of passing chips in data-password-strength, so a stylesheet can draw a strength bar. NDS ships no bar.

  • Programmatic Control

    Register rules, check the value again, and read the current result through the JS API.

Best Practices

  • Use it on sign-up, change-password and password-reset forms, where the user picks a new password.
  • Add one chip for each rule the server enforces. A chip the server ignores shows the user a rule that is not real.
  • Keep minlength on the input, and write the same number in the length chip’s text. The browser enforces minlength, so the length rule still blocks the submit before the script loads.
  • Put .nds-password-rules inside the data-feedback-target footer, and give every chip data-permanent and data-status="neutral". A validation message then takes the chips’ place, and the chips come back when it clears. The script loads after first paint, so a chip without data-status has no style until then.
  • Point the input’s aria-describedby at .nds-password-rules, so a screen reader reads the rules on focus. Keep the .nds-password-status region: without it, a screen reader hears no progress.
  • Write each chip message as the rule, such as “One number (0-9)”, not as an error. The chip turns red on its own.
  • Use data-rule-pattern for a one-off rule, not addRule(). The expression then sits next to the message the user reads.
  • Add a hidden username field with autocomplete="username" above the password fields, so a password manager saves the right account.
  • Do not treat the strength value as a security measure. It counts chips, not how hard the password is to guess.
  • For the label, info text and feedback, see Forms. They work the same on every field.

API

Data Attributes

Attribute Element Effect
data-rule .nds-password-rules > .nds-feedback The rule the chip checks: length, upper, lower, digit, special or match. Another name needs data-rule-pattern or addRule(). Without one, the chip stays neutral and is not counted
data-rule-pattern .nds-password-rules > .nds-feedback A regular expression the value must match. It wins over a built-in rule with the same name
data-status .nds-password-rules > .nds-feedback Write neutral. The script sets success or error on each keystroke
data-permanent .nds-password-rules > .nds-feedback A validation message hides the chip instead of removing it. The chip comes back when the message clears. Without it, the first error removes the chips for good
data-password-match .nds-password A CSS selector for the first password input. It makes the field a retype field. The script reads it once at start. When the first field is added later, call NDS.Password.destroy() and then NDS.Password.create() on the retype field
minlength the <input> The length rule’s minimum. The default is 8
data-password-strength .nds-password Written by the script: the number of passing chips, 0 while the field is empty. Style a bar from it, such as .nds-password[data-password-strength="5"] .my-bar { width: 100%; }

JavaScript

The NDS.Password script loads after the page shows. Until it loads, each call loads it and returns a promise of the result, so await a call whose result you use.

The instance is on the field’s .nds-form-container as ndsPassword. It is there once the component starts: code that runs at page load waits for NDS.init.ready.

Method Effect
NDS.Password.init() Starts every password field on the page that has not started. Call it after you add a field
NDS.Password.create(container) Starts one field: pass its .nds-form-container. Returns the instance, or null when the field has no password input, or no chips and no match
NDS.Password.destroy(container) Stops one field, removes its submit block and sets its chips back to neutral
NDS.Password.check(container) Checks the value now, fires nds:password:change and returns { strength, allPass, rules }
NDS.Password.addRule(name, test) Registers a rule. test(value, ctx) returns true when the value passes. ctx.minLength is the length minimum, and ctx.matchValue is the first field’s value, or null with no match. Chips with that data-rule count from the next keystroke
NDS.Password.getValue(container) Returns the text in the password input, or '' when the field is not found. container is the .nds-form-container, its input, its id or a selector
NDS.Password.setValue(container, value) Writes the text as a user would, so the rules check again. Returns true, or false when the field is not found
NDS.Password.clear(container) Empties the input. Returns true, or false when the field is not found
container.ndsPassword.getValue(), .setValue(value), .clear() The same, on a field that has started
container.ndsPassword.getStrength() Returns the last result, with no new check and no event
Event Fired on Detail
nds:password:change .nds-password, and it bubbles { strength, allPass, rules }. It fires at start and on each input. On a retype field, it also fires when the first field changes. strength is the number of passing chips. allPass is true when every counted chip passes and the two values match. It is false while the field is empty. rules maps each rule name to true or false. The detail holds no password: read the input when you need it
// A rule that the password must not be the username NDS.Password.addRule('nouser', function (value) { return value.toLowerCase() !== document.querySelector('#username').value.toLowerCase(); }); // Show the strength as text var field = document.querySelector('#new-password').closest('.nds-password'); field.addEventListener('nds:password:change', function (e) { document.querySelector('#strength-text').textContent = e.detail.strength + ' of 5'; }); // Wire a field added after page load NDS.Password.init();

The full API is in the banner of _js/nds-password.js.

Last Modified Date: 05/11/2026 - 03:30 PM
Was this page useful?
60% of users said Yes from 2843 Feedbacks