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:
Markup
Variants
A Min length choice sets minlength on the input and swaps the length chip for one that names the new number. Each Validation row adds its rule chip to the first field’s rules, and any mix of them can be on. On a page, add a rule chip to the .nds-password-rules of any field.
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Structure | New password (default) | — | — | One field with its rule chips, for sign-up or a password reset |
| Structure | Confirm (hint: A retype field that must match the first) | canon #password-confirm |
— | A new password and a retype field. The retype field’s chip checks that the two values match |
| Min length | 10 | [minlength="10"] |
#new-password |
A longer minimum, for an account that holds sensitive data |
| Min length | 4 | [minlength="4"] |
#first-password |
The same, on the first field of Confirm |
| Min length | 4 | canon #password-length-4 |
[data-rule="length"] (after) |
The length chip with the new number |
| Min length | 4 | remove | [data-rule="length"]:first-child |
Removes the old length chip |
| Validation (any) | Capital letter | canon #password-rule-upper |
#new-password-rules |
The password needs one letter from A to Z |
| Min length | 6 | [minlength="6"] |
#first-password |
The same, on the first field of Confirm |
| Min length | 6 | canon #password-length-6 |
[data-rule="length"] (after) |
The length chip with the new number |
| Min length | 6 | remove | [data-rule="length"]:first-child |
Removes the old length chip |
| Min length | 4 | [minlength="4"] |
#new-password |
A very short minimum, such as a PIN-style password. Pair it with more rules |
| Min length | 6 | [minlength="6"] |
#new-password |
A shorter minimum. Pair it with more rules |
| Min length | 10 | [minlength="10"] |
#first-password |
The same, on the first field of Confirm |
| Min length | 10 | canon #password-length-10 |
[data-rule="length"] (after) |
The length chip with the new number |
| Min length | 10 | remove | [data-rule="length"]:first-child |
Removes the old length chip |
| Min length | 8 (default) | — | — | The length rule’s minimum, in minlength on the input and in the length chip’s text |
| Validation (any) | Capital letter | canon #password-rule-upper |
#first-password-rules |
The same, on the first field of Confirm |
| Validation (any) | Small letter | canon #password-rule-lower |
#new-password-rules |
The password needs one letter from a to z |
| Validation (any) | Small letter | canon #password-rule-lower |
#first-password-rules |
The same, on the first field of Confirm |
| Validation (any) | Number | canon #password-rule-digit |
#new-password-rules |
The password needs one number from 0 to 9 |
| Validation (any) | Number | canon #password-rule-digit |
#first-password-rules |
The same, on the first field of Confirm |
| Validation (any) | Symbol | canon #password-rule-special |
#new-password-rules |
The password needs one character that is not a letter or a number |
| Validation (any) | Symbol | canon #password-rule-special |
#first-password-rules |
The same, on the first field of Confirm |
| Validation (any) | No spaces | canon #password-rule-space |
#new-password-rules |
A chip whose rule is the regular expression in data-rule-pattern. It needs no addRule() call |
| Validation (any) | No spaces | canon #password-rule-space |
#first-password-rules |
The same, on the first field of Confirm |
| Field states | Label, info, feedback, required | — | — | Shared by every form field. See Forms |
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-passwordfield 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
minlengthon the input, and write the same number in the length chip’s text. The browser enforcesminlength, so the length rule still blocks the submit before the script loads. - Put
.nds-password-rulesinside thedata-feedback-targetfooter, and give every chipdata-permanentanddata-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 withoutdata-statushas no style until then. - Point the input’s
aria-describedbyat.nds-password-rules, so a screen reader reads the rules on focus. Keep the.nds-password-statusregion: 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-patternfor a one-off rule, notaddRule(). 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 |
The full API is in the banner of _js/nds-password.js.