Validate that a password contains an uppercase letter and either a number or a symbol

Validate that a password contains an uppercase letter and either a number or a symbol

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

Stefan Bogdanescu

Founder & Senior Architect

Validate that a password contains an uppercase letter and either a number or a symbol

Validating password complexity often boils down to checking for specific character classes rather than analyzing the string's structure. You need to ensure there is at least one uppercase letter, and additionally, at least one digit or special character. The most robust way to achieve this in PHP is using regular expressions with negative lookahead assertions. This approach avoids the need to iterate through every character manually, which can be inefficient and prone to edge-case errors like failing to handle multi-byte characters correctly.

The core logic requires two conditions to be met simultaneously:

  1. The string must contain at least one uppercase letter [A-Z].
  2. The string must contain either a number ([0-9]) or a symbol (anything outside of letters and numbers).

You can express this cleanly using the preg_match function combined with lookahead assertions. A negative lookahead (?!...) asserts that a pattern does not match at the current position, effectively forcing the regex engine to find a character that is not the excluded set before moving on.

Here is a practical implementation using native PHP functions:

function validatePasswordStrength(string $password): bool {
    // Check for at least one uppercase letter
    if (!preg_match('/[A-Z]/', $password)) {
        return false;
    }

    // Check for at least one number OR one symbol
    // The pattern (?![0-9]) ensures we are not currently looking at a digit.
    // The pattern (?!\p{L}|\p{N}) ensures we are not looking at any letter or number (symbol).
    // We use \p{L} for Unicode letters and \p{N} for Unicode numbers to be safe, 
    // but [0-9A-Za-z] is sufficient for standard ASCII requirements.
    
    $hasNumberOrSymbol = preg_match('/(?![0-9])(?![A-Za-z])/u', $password);
    
    if ($hasNumberOrSymbol !== 1) {
        return false;
    }

    return true;
}

While this works, a more concise single-expression alternative exists using positive lookaheads. This pattern asserts that the string contains an uppercase letter AND contains a non-alphanumeric character (or digit):

function validatePasswordStrength(string $password): bool {
    // Requires at least one [A-Z] and at least one char that is NOT [a-zA-Z0-9]
    return preg_match('/(?=.*[A-Z])(?=.*[^a-zA-Z0-9])/', $password);
}

If your requirements allow digits to count as the "second" requirement (as implied by "number or symbol"), you adjust the negative character class slightly. The pattern [^a-zA-Z] matches anything that isn't a letter, which includes both numbers and symbols. However, since we strictly require an uppercase letter first, we must ensure the second condition captures digits specifically if needed.

Let's refine the logic to strictly match "uppercase" + "number OR symbol":

function validatePasswordStrength(string $password): bool {
    // 1. Must have at least one Uppercase [A-Z]
    // 2. Must have at least one Non-Lowercase (which covers Upper, Numbers, Symbols)
    // This is tricky because we want to separate Upper from the rest.
    
    // Better approach: 
    // Assert exists an uppercase letter.
    // Assert exists a character that is NOT a lowercase letter AND NOT an uppercase letter (i.e., Number or Symbol).
    
    return preg_match('/(?=.*[A-Z])(?=.*[^a-zA-Z])/', $password);
}

Wait, the pattern [^a-zA-Z] includes numbers and symbols. If the password is "Password1", does it pass?

  • Has Uppercase? Yes ('P').
  • Has non-letter? Yes ('1').
    So "Password1" passes. This matches the requirement "uppercase AND (number OR symbol)".

Why not iterate characters?

The source material mentions splitting a string into loose characters to check them individually, similar to an ActionScript loop. While valid in languages where array iteration is cheap, regex lookaheads are generally preferred in PHP for password validation because they handle the state machine logic internally without creating intermediate arrays. Splitting a string in PHP using str_split() creates an indexed array of single-character strings. Iterating over this array adds function call overhead and memory allocation for every character check.

Furthermore, manual iteration makes it difficult to handle Unicode normalization or multi-byte characters (like accented letters) unless you explicitly set the internal encoding to UTF-8 and iterate by byte offset carefully. Regular expressions with the u modifier handle these complexities automatically.

Trade-offs and Pitfalls

One common mistake is relying solely on preg_match('/[A-Z]/') and assuming that anything else is a symbol. You must explicitly exclude alphanumeric characters in your second check if you want to distinguish between numbers and symbols, or group them together as acceptable alternatives to letters.

Another pitfall is performance. While regex is fast, compiling the same pattern repeatedly inside a loop (e.g., validating thousands of passwords in a tight loop) can degrade performance. If you are building a reusable library, consider caching the compiled pattern using preg_match() with the PREG_PATTERN_ORDER flag or simply ensuring the pattern string is static outside the function scope if possible.

For production environments where security is paramount, ensure you also validate length requirements separately, as regex can become unreadable when trying to enforce multiple length constraints simultaneously. Always hash the password (using password_hash) before storing it, and never log the raw password or the result of a failed validation in a way that leaks information about the specific character classes required.

If you are building this for a Laravel application, you can wrap this logic in a custom Rule class or use the Validator facade directly within your controller or form request. The underlying mechanism remains the same: precise pattern matching to enforce complexity without unnecessary string manipulation overhead.

Our network