Mobile number validation pattern in PHP

Mobile number validation pattern in PHP

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

Stefan Bogdanescu

Founder & Senior Architect

Mobile number validation pattern in PHP

Validating user input is a fundamental requirement for any web application, yet it often reveals deep misunderstandings about regex patterns and character classes. A common scenario involves distinguishing between email formats and mobile numbers. While validating an email address like user@example.com might seem straightforward, enforcing a strict 10-digit format for mobile numbers (e.g., 1234567890) introduces specific challenges that standard regex patterns often mishandle.

Consider a typical implementation attempt where a developer tries to isolate numeric characters:

function validate_mobile($mobile) {
    return eregi("/^[0-9]*$/", $mobile);
}

This approach is fundamentally flawed because it accepts strings of zero digits, such as an empty string "". In a production environment, allowing an empty value fails to enforce the requirement for exactly 10 digits. The pattern [0-9]* means "zero or more digits," whereas we need "exactly ten digits." Additionally, relying on the deprecated eregi() function is unnecessary in modern PHP versions; using preg_match() provides better performance and access to extensive documentation.

To correctly validate a fixed-length mobile number, the quantifier must be adjusted from * (zero or more) to {10} (exactly ten). The corrected pattern becomes:

function validate_mobile($mobile) {
    return preg_match('/^[0-9]{10}$/', $mobile);
}

Here, the ^ anchor asserts the start of the string, and $ asserts the end. This ensures that no extra characters exist before or after the ten digits. If the input is 1234567890, it passes. If it is 123456789, it fails because the count is insufficient. If it is 12345678901, it fails because the count exceeds the limit.

However, strict numeric validation is only part of the equation. Real-world mobile numbers often include formatting characters like dashes or spaces (e.g., 123-456-7890). If your application requires a specific format such as 1234567890 without separators, the regex must be strict. If you need to accept optional separators, the pattern becomes more complex:

return preg_match('/^[0-9]{3}[- ]?[0-9]{3}[- ]?[0-9]{4}$/', $mobile);

This variation allows for hyphens or spaces between the first three digits and the next three, but still enforces a total of 10 digits. It also rejects inputs with leading zeros if they are not part of the intended format, depending on how you define valid mobile prefixes.

Another critical consideration is internationalization. A simple 10-digit pattern assumes a specific country code context (often India or similar). If your application needs to handle global numbers, relying solely on digit count is insufficient. You would need to incorporate Country Code (CC) logic, potentially using a lookup table against the libphonenumber library rather than a raw regex.

It is also worth noting that email validation functions often use overly permissive patterns to maximize success rates. For example:

function validate_email($email) {
    return preg_match('/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/', $email);
}

While this works for basic cases, it does not strictly validate RFC 5322 compliance. Similarly, mobile validation should balance strictness with usability. If you are building a form where user experience matters, consider normalizing the input by stripping non-digit characters before applying the regex check. This decouples the UI from the backend logic and simplifies the validation rule to just "is it 10 digits?".

For developers working within the Laravel ecosystem, you might find that standard validation rules do not cover specific length requirements for mobile numbers out of the box. While packages like illuminate provide robust tools, custom validators are often necessary for niche constraints like exact digit counts. If you require more sophisticated phone number handling, including country code detection and format normalization, exploring libraries such as the one provided by LaravelCompany can offer pre-built utilities that abstract away these regex complexities.

Ultimately, the choice between a rigid regex and a library depends on your project's scale and the diversity of phone formats you must support. For simple 10-digit constraints, a precise quantifier in preg_match() is the most efficient solution.

Our network