REGEXVAULTv2.0
Security/Password Formats

Password Policy Strength Check Regex for PHP

/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~`])(?!.*\s).{12,128}$/

What this pattern does

This page provides a comprehensive, battle-tested regular expression for matching password policy strength check, ported and verified for PHP. In security-sensitive code, using an unverified regex can open the door to both false positives and denial-of-service attacks. The snippet below is ready to drop into your PHP project — whether you're validating in a Laravel validator, a WordPress plugin, or a standalone PHP script.

Php Implementation

Php
<?php
// Password Policy Strength Check
// ReDoS-safe | RegexVault — Security > Password Formats

define('PASSWORD_POLICY_STRENGTH_CHECK_PATTERN', '/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*()_+\-=\[\]{};\':"\\|,.<>\\/?~`])(?!.*\s).{12,128}$/');

function validate_password_policy_strength_check(string $input): bool {
    return (bool) preg_match(PASSWORD_POLICY_STRENGTH_CHECK_PATTERN, $input);
}

// Example
var_dump(validate_password_policy_strength_check("Correct!Horse#Battery9")); // bool(true)

Test Cases

Matches (Valid)
Rejects (Invalid)
Correct!Horse#Battery9password123
P@ssw0rd_Secur3!Password1
MyStr0ng!Password#2024SHORT!1Aa
NoSpecialChar123
Has Spaces!A1b

When to use this pattern

This pattern is drawn from the Security > Password Formats category and is provided for complex validation requirements. That matters for PHP developers because especially relevant in PHP where PCRE backtracking limits can trigger silent failures on malicious input. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.

Common Pitfalls

NIST 800-63b now recommends checking passwords against breach databases (HaveIBeenPwned Pwned Passwords API) rather than enforcing arbitrary complexity. Long passphrases that fail complexity checks are often stronger than short complex passwords.

Technical Notes

NIST SP 800-63b (2017) de-emphasized complexity rules in favor of length and breach database checking. This pattern implements the more traditional complexity approach. Minimum 12 characters is NIST-aligned. 128-char maximum prevents DoS via extremely long inputs.

Have a pattern that belongs in the vault?

Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.

Submit a Pattern