What Is a Good Regex for Strong Password Validation?

Pattern: ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$. This regex enforces a minimum password strength by requiring at least one lowercase letter, one uppercase letter, one digit, one special character, and a minimum length of 8 characters. While widely used, modern security guidance (NIST SP 800-63B) recommends prioritizing password length over complexity rules.

Breaking Down the Pattern

PartMeaning
^Start of string
(?=.*[a-z])Lookahead: at least one lowercase letter
(?=.*[A-Z])Lookahead: at least one uppercase letter
(?=.*\d)Lookahead: at least one digit
(?=.*[@$!%*?&])Lookahead: at least one special character
[A-Za-z\d@$!%*?&]{8,}$At least 8 characters from the allowed set

Test Cases

InputMatch?Note
MyP@ssw0rdYesValid format
Str0ng!PassYesValid format
C0mpl3x&PwYesValid format
weakpasswordNoNo uppercase, digit, or special character
Short1!NoOnly 7 characters (minimum is 8)

Usage Examples

JavaScript

const pattern = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/;
pattern.test('MyP@ssw0rd');   // true
pattern.test('weakpassword');   // false

Python

import re
pattern = r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$'
bool(re.match(pattern, 'MyP@ssw0rd'))  # True
bool(re.match(pattern, 'weakpassword'))  # False

Common Pitfalls

Try It Yourself

Test this regex with our Regex Tester.

Frequently Asked Questions

Are complex password rules actually more secure?

Not necessarily. NIST research shows that complexity rules (uppercase, special chars, etc.) often lead users to create predictable patterns like "Password1!" or write passwords down. Length is a stronger factor -- a 16-character passphrase is more secure than an 8-character complex password.

What minimum password length should I enforce?

NIST recommends a minimum of 8 characters, with 12-16 being ideal. The maximum should be at least 64 characters to allow passphrases. Never truncate passwords silently.

Should I check passwords against breach databases?

Yes. Services like Have I Been Pwned offer an API to check if a password has appeared in known data breaches. This is more effective than complexity rules.

Built by Michael Lip. 100% client-side — no data leaves your browser.