Password Security
Regular Expressions
Coding Standards
Data Validation
Digital Safety

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.

Practice system design

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:

regex
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^A-Za-z0-9]).{8,}$

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

javascript
1function isValidPassword(password) {
2  const regex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^A-Za-z0-9]).{8,}$/;
3  return regex.test(password);
4}
5
6console.log(isValidPassword("Abcdef1!")); // true
7console.log(isValidPassword("abcdef1!")); // false

This is a common client-side validation pattern, though you should still validate on the server as well.

Example in Python

python
1import re
2
3pattern = re.compile(r"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^A-Za-z0-9]).{8,}$")
4
5print(bool(pattern.fullmatch("Abcdef1!")))
6print(bool(pattern.fullmatch("abcdef1!")))

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:

regex
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*]).{8,}$

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.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design