PHP FILTER_VALIDATE_EMAIL does not work correctly

PHP FILTER_VALIDATE_EMAIL does not work correctly

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

Stefan Bogdanescu

Founder & Senior Architect

PHP FILTER_VALIDATE_EMAIL does not work correctly

When validating email addresses in PHP, developers often rely on the built-in filter_var() function with the FILTER_VALIDATE_EMAIL filter. It is a convenient approach that saves you from writing complex regular expressions manually. However, as demonstrated by a specific edge case involving top-level domains, this filter has limitations that can lead to validation errors if your requirements are strict.

Consider a scenario where you are processing user input and encounter the string test@example.c. Intuitively, a domain ending in a single letter like .c is structurally invalid because no officially recognized top-level domain exists with only one character. According to the authoritative list maintained by IANA (http://data.iana.org/TLD/tlds-alpha-by-domain.txt), valid TLDs generally consist of at least two letters (e.g., .com, .org, .net).

Despite this logical expectation, running the following code in PHP 5.3.10 yields a result that contradicts the standard:

$email = "test@example.c";
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo "Email: " . $email . " correct";
} else {
    echo "email not correct";
}

The output is Email: test@example.c correct. The filter accepts an address with a single-character TLD as valid. This behavior indicates that the default implementation of FILTER_VALIDATE_EMAIL does not strictly enforce the existence or length constraints of the top-level domain against the official IANA registry. Instead, it relies on a regular expression pattern defined within PHP's core filters.

How the Filter Works Under the Hood

The underlying logic for FILTER_VALIDATE_EMAIL uses a specific regular expression to parse the email syntax. This regex checks for:

  1. A valid local part (before the @).
  2. The @ symbol.
  3. A valid domain name structure.

In older versions of PHP, including 5.3.10, the domain validation logic was relatively permissive regarding the top-level domain. It typically required the TLD to be between two and six characters long but did not cross-reference a live list of available domains. Consequently, example.c passes because it meets the character count rules (1 is often treated loosely or the regex allows 2-6, but historical regex variations sometimes allowed edge cases or the specific version used had a pattern like [a-zA-Z]{2,6} which might have been interpreted differently depending on the exact internal implementation of that specific PHP release).

However, looking at the source code behavior for FILTER_VALIDATE_EMAIL in various versions reveals that it validates the format of the domain but not necessarily the registry existence or strict adherence to current IANA lists. If your application requires strict compliance with RFC 5321 and current TLD standards, relying solely on this filter is risky.

Implementing Strict Validation

If you need to reject emails like test@example.c, you must augment the built-in validation. The most robust approach involves a two-step process:

  1. Use filter_var() as a first-pass check to catch obvious syntax errors (missing @, invalid characters in the local part).
  2. Perform a secondary check against a list of valid TLDs or use a more restrictive regex pattern that explicitly enforces a minimum TLD length of two characters.

You can create a helper function that combines these checks:

function validateStrictEmail($email) {
    // First, use the built-in filter to ensure basic syntax validity
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        return false;
    }

    // Extract the domain part
    $parts = explode('@', $email);
    if (count($parts) !== 2) {
        return false;
    }

    $domain = $parts[1];
    $tld = substr(strrchr($domain, '.'), 1); // Get the part after the last dot

    // Enforce that the TLD must be at least 2 characters long
    if (strlen($tld) < 2) {
        return false;
    }

    return true;
}

By adding the length check for the TLD, you effectively filter out addresses like test@example.c while still accepting standard formats. This approach balances the convenience of PHP's built-in filters with the necessity of business logic constraints.

Trade-offs and Considerations

Using a hardcoded list of valid domains or performing DNS lookups to verify every email is computationally expensive and often unnecessary for general validation. Most security-focused frameworks recommend a hybrid approach: use regex or filter functions for syntax, but avoid strict TLD existence checks unless you are dealing with high-security contexts (like banking applications).

For web development frameworks like Laravel, which often handle form validation heavily, you might find that the default ruleset provided by such platforms is stricter. If you are using a framework that allows custom validators, integrating a stricter rule here ensures consistency across your application stack without reinventing the wheel for every single input field.

Our network