SPONSOR: Cloud Hosting for Developers • Scale your apps globally with zero configuration. [Ad Placement Zone]

Password Strength Validation Regex

Enforce password complexity requirements using positive lookahead assertions. Test characters, numbers, symbols, and length restrictions in real-time.

Loading editor...

How to Enforce Password Rules in One Regex

Password validation typically requires checking for multiple independent conditions: at least one lowercase letter, one uppercase letter, one digit, and one special character. Doing this with standard regex would require listing every possible permutation of these characters.

Instead, we use Positive Lookahead Assertions ((?=...)). A lookahead asserts that a sub-pattern matches the current position without consuming characters or moving the engine's match pointer forward. By placing lookaheads at the start of the string (^), we can scan the entire string for multiple conditions sequentially before validating the length.

Breaking Down the Lookahead Pattern

Let's dissect the standard strong password regex pattern:

/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/
  • Lowercase check ((?=.*[a-z])): Scans forward to find at least one lowercase letter.
  • Uppercase check ((?=.*[A-Z])): Scans forward to find at least one uppercase letter.
  • Number check ((?=.*\d)): Scans forward to find at least one numeric digit (0-9).
  • Special character check ((?=.*[@$!%*?&])): Scans forward to find at least one character from the special set.
  • Allowed characters and length ([A-Za-z\d@$!%*?&]{8,}): Consumes characters from the permitted set, ensuring there are at least 8 characters in total.