How the pattern works
| Part | Checks |
|---|---|
(?=.*[a-z]) | There is a lowercase letter. |
(?=.*[A-Z]) | There is an uppercase letter. |
(?=.*\d) | There is a digit. |
(?=.*[^A-Za-z0-9]) | There is a character that isn't a letter or digit, such as a symbol or a space. |
.{12,} | The password is at least 12 characters long. |
Show which rule failed
One big regex can only say yes or no. Separate checks let you tell people exactly what's missing:
const rules = [
[/.{12,}/, "at least 12 characters"],
[/[a-z]/, "a lowercase letter"],
[/[A-Z]/, "an uppercase letter"],
[/\d/, "a number"],
[/[^A-Za-z0-9]/, "a symbol"],
];
const missing = (password) => rules.filter(([rule]) => !rule.test(password)).map(([, label]) => label);
missing("Short1!"); // ["at least 12 characters"]What current guidance recommends
- NIST's digital identity guidelines (SP 800-63B) favour length over composition rules: require a minimum length, allow long passphrases and all characters, including spaces, and don't force symbols or regular changes.
- Check new passwords against lists of known breached passwords, such as the Have I Been Pwned Pwned Passwords API.
- Never cap the length low, and never strip or reject characters; hash the password with Argon2id, scrypt or bcrypt instead.
Frequently asked questions
Why does correcthorsebattery fail?
It has no uppercase letter, digit or symbol. It is long, though, which is why length-based policies would accept it: four random words are hard to guess.
Does this pattern allow spaces?
Yes. . matches any character except a line break, and a space counts as a symbol for the last lookahead.
How do I require 8 characters instead of 12?
Change {12,} to {8,}. Twelve or more is a better minimum for new systems.