Password Validation Regular Expression
Password validation is policy-driven. Some forms need a strict complexity rule, while others only need reasonable length and no whitespace. These examples show three common options so you can pick the one that matches your product instead of forcing a single policy everywhere.
Validation Options
Strict- 8 to 64 characters, at least one lowercase letter, one uppercase letter, one digit, one special character, and no whitespace.Balanced- 8 to 64 characters, at least one letter and one digit, special characters optional, and no whitespace.Minimal- 8 to 64 non-whitespace characters with no composition requirements.
Strict Validation
Use this policy when you need explicit complexity requirements: lowercase, uppercase, digit, special character, 8 to 64 characters, and no whitespace.
Explanation
^- Start of the string.(?=.*[a-z])- Requires at least one lowercase ASCII letter.(?=.*[A-Z])- Requires at least one uppercase ASCII letter.(?=.*\d)- Requires at least one digit.(?=.*[!@#$%^&*(),.?":{}|<>_\-\[\]\\\/+=~';])- Requires at least one special character from the allowed set.[A-Za-z\d!@#$%^&*(),.?":{}|<>_\-\[\]\\\/+=~';]{8,64}- Allows only the listed ASCII characters and enforces a total length between 8 and 64 characters.$- End of the string.
Note: Regex can enforce a password format, but it does not make password storage safe. Store passwords with a dedicated password hashing algorithm such as Argon2, scrypt, or bcrypt, and consider checking for compromised or commonly used passwords separately.
Practical guidance: Use the strict option when a policy explicitly requires character classes. Use the balanced option when you want basic resistance to trivial passwords without forcing symbols or mixed case. Use the minimal option when the main requirement is length and you expect users to rely on password managers.
Implementation
Test Cases
| Input | Valid |
|---|---|
| Str0ng!Pass | |
| A1!bcdef | |
| P@ssw0rd2026 | |
| Valid_123! | |
| N0Spaces# | |
| lowercase1! | |
| UPPERCASE1! | |
| NoNumber! | |
| NoSpecial1 | |
| Sh0rt! | |
| Has Space1! | |
| Tabs Pass1! | |
| Äbcd123! | |
| Password123 | |
| (empty string) | |
| aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaA1! |
Balanced Validation
Use this when you want a lighter policy: at least one letter, at least one digit, 8 to 64 characters, and no whitespace. Special characters are allowed but not required.
Explanation
^- Start of the string.(?=.*[A-Za-z])- Requires at least one ASCII letter.(?=.*\d)- Requires at least one digit.\S{8,64}- Requires 8 to 64 non-whitespace characters.$- End of the string.
Implementation
Test Cases
| Input | Valid |
|---|---|
| password1 | |
| Passw0rd | |
| abc12345 | |
| 1234test! | |
| LOGIN2026 | |
| NoDigitsHere | |
| 12345678 | |
| short1a | |
| has space1 | |
| tabs 123a | |
| (empty string) | |
| aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1 |
Minimal Validation
Use this when the main requirement is length and no spaces. It accepts any non-whitespace characters from 8 to 64 characters long and leaves strength decisions to users or password managers.
Explanation
^- Start of the string.\S{8,64}- Requires 8 to 64 non-whitespace characters.$- End of the string.
Implementation
Test Cases
| Input | Valid |
|---|---|
| password | |
| 12345678 | |
| long-enough | |
| UPPERCASE | |
| Abc123!@ | |
| short | |
| has space | |
| tabs pass | |
| (empty string) | |
| aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |