Regex for password must contain at least eight characters, at least one number and both lower and uppercase letters and special characters
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
A common password rule requires at least eight characters, one lowercase letter, one uppercase letter, one digit, and one special character. Regex can enforce that structure with positive lookaheads, but it is important to remember that regex checks format only; it does not guarantee that the password is truly strong.
A Typical Regex
One common pattern looks like this:
This means:
- '
^start of string' - '
(?=.*[a-z])at least one lowercase letter' - '
(?=.*[A-Z])at least one uppercase letter' - '
(?=.*\d)at least one digit' - '
(?=.*[^A-Za-z0-9])at least one non-alphanumeric character' - '
.{8,}total length at least eight' - '
$end of string'
Each lookahead checks a rule without consuming characters, and the final length pattern ensures the whole string is long enough.
Example in JavaScript
This is a common client-side validation pattern, though you should still validate on the server as well.
Example in Python
Using fullmatch is a good way to ensure the entire password string satisfies the pattern.
Choosing the Special Character Rule
The pattern [^A-Za-z0-9] means "any non-letter and non-digit." That is broader than a hardcoded set such as [!@#$%^&*].
If your policy wants only a specific set of special characters, write that explicitly:
The broader form is more flexible, while the narrower form is more restrictive and predictable. The right choice depends on your password policy and UI guidance.
Regex Is Only the First Layer
A password can match the regex and still be weak:
- '
Password1!' - '
Qwerty1!' - '
Abcdefg1!'
These satisfy the shape rules but may still be guessable or common. Good password systems usually combine regex-like policy checks with:
- breached-password screening
- rate limiting
- MFA
- user guidance toward passphrases or strong generated passwords
So regex should be seen as input validation, not as a full security strategy.
Be Careful With Error Messages
If you reject a password, the UI should tell the user which rule failed. Regex itself only yields true or false, so applications often pair the regex with clearer rule checks or a friendly checklist in the interface.
That gives users a better experience than a generic "invalid password" message.
Test the Pattern With Real Samples
Before shipping the rule, validate it with representative accepted and rejected passwords. Small regex mistakes are easy to make and hard to spot by visual inspection alone.
Common Pitfalls
The biggest mistake is treating a regex policy as equivalent to password security. It only enforces visible composition rules.
Another issue is using an overly narrow special-character set without telling users which characters are accepted. That leads to frustrating validation failures.
Developers also forget that front-end validation is not enough. The same rules must be enforced on the server.
Finally, regex alone produces poor feedback. If usability matters, pair the validation with specific messages about missing character classes or length requirements.
Summary
- Positive lookaheads are the usual regex technique for password composition rules.
- A common pattern can enforce lowercase, uppercase, digit, special character, and minimum length requirements.
- Decide whether the special-character rule should allow any non-alphanumeric character or only a specific set.
- Regex validates format, not real password strength.
- Always enforce password rules on the server, not only in the client UI.
Related reading
- Remove all the Kafka ACLs
- Remove credentials from Git
- Remove sensitive files and their commits from Git history
- Remove Using default security password on Spring Boot
- Rename an IAM Role
- Repository is not signed in docker build
- required a bean of type 'org.springframework.security.core.userdetails.UserDetailsService' that could not be found
- Reset MySQL root password using ALTER USER statement after install on Mac
System design interview problems on this topic
- Design a Secure Identity Management System: solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
- Design a Network Security Monitoring Tool: solution and walkthrough
- Design a CAPTCHA System: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.