Create preg_match for password validation allowing (!@#$%)

Create preg_match for password validation allowing (!@#$%)

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Building a Robust Password Regex for Special Characters

Validating passwords with preg_match requires more than just listing allowed characters; it demands a precise understanding of how regular expression quantifiers and character classes interact. The specific challenge here is allowing the set !@#$% while simultaneously enforcing length constraints (8–12 characters) and complexity requirements (at least one letter and one number). A naive approach often leads to regex that is either too permissive or syntactically incorrect when handling special symbols inside character classes.

To construct the pattern, we first need to define the character set precisely. The allowed characters are alphanumeric values plus !, @, #, $, and %. In a PHP character class defined by square brackets [...], most of these symbols do not require escaping. However, the dollar sign ($) is a special anchor in regex that matches the end of a string outside of a character class, so it must be escaped as \$ inside the brackets to be treated as a literal character. The exclamation mark, at-sign, hash, and percent are safe to include literally.

The resulting character class looks like this: [a-zA-Z0-9!@#$\%]. Note that while % is generally safe in many contexts, escaping it explicitly (\%) is often a good habit to avoid ambiguity if the regex engine version changes or if you plan to extend the rule set later. We will use [a-zA-Z0-9!@#$\%]{8,12} to enforce the minimum and maximum length constraints directly on the character class.

Next comes the complexity requirement: the password must contain at least one letter and at least one number. While it might be tempting to try to assert this within a single lookahead or using complex quantifiers like (?=.*[a-zA-Z])(?=.*\d), modern best practices suggest breaking this logic into multiple assertions for clarity and maintainability. We can use positive lookaheads, which allow us to check conditions without consuming characters from the string. The pattern would start with ^(?=(.*[a-zA-Z]))(?=(.*\d)) to ensure there is at least one alphabetic character and at least one digit somewhere in the sequence.

Combining these elements, the full regex pattern becomes:

'^(?=.*[a-zA-Z])(?=.*\d)[a-zA-Z0-9!@#$\%]{8,12}$'

This pattern asserts start (^) and end ($) anchors to ensure the entire string matches the criteria. The lookaheads check for presence first, then the character class with quantifiers validates the composition and length.

When implementing this in PHP, the logic integrates cleanly into a function:

if (!preg_match('/^(?=.*[a-zA-Z])(?=.*\d)[a-zA-Z0-9!@#$\%]{8,12}$/', $password)) {
    // Trigger validation error
}

One common pitfall developers encounter is forgetting to escape the dollar sign inside the character class or miscounting the length range. Another subtle issue arises if you attempt to make the regex case-insensitive using the i flag; while convenient, it can sometimes obscure which specific characters are being matched if debugging later. Explicitly defining [a-zA-Z] ensures no ambiguity about case sensitivity unless explicitly desired.

For larger applications, especially those built with Laravel, such validation logic is often centralized in a validator class rather than scattered across multiple functions. This promotes reusability and makes it easier to tweak rules like the allowed special characters without hunting through multiple files. If you are working within a Laravel project, consider leveraging the native Validator facade or creating a custom rule method that encapsulates this regex logic, ensuring consistency across your entire application stack.

Our network