Laravel validation for a number which should be exactly 10 digits

Laravel validation for a number which should be exactly 10 digits

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

Stefan Bogdanescu

Founder & Senior Architect

Laravel Validation for Exactly 10 Digits

Validating a phone number in Laravel often feels like trying to force a square peg into a round hole. You need a field that accepts only numbers, but you also need strict length requirements. The specific constraint of "exactly 10 digits" is common in regions using the E.164 standard or older national formats, yet standard Laravel validators can make this surprisingly tricky if not configured correctly.

The core issue lies in how Laravel interprets validation rules for numbers versus strings. In your scenario, you are attempting to enforce a strict length of 10 characters on a field representing a phone number. The most robust approach is to treat the input as a string during validation rather than relying solely on the numeric rule, which can sometimes behave unexpectedly with leading zeros or specific formatting expectations.

To enforce an exact digit count, you should combine the required rule with the digits validator. This ensures that the input contains only digits and matches the length precisely. If you attempt to use between:9,11 as shown in your controller code, Laravel will accept any number between 9 and 11 digits. This is a range check, not an exact match. Consequently, an input of '555-1234' (8 characters) might pass if the dashes are ignored by the numeric caster, or a 11-digit number might slip through if you aren't careful about the upper bound.

Here is how to refactor your controller method to strictly enforce exactly 10 digits:

public function tytraining(Request $request)
{
    $this->validate($request, [
        'txt_name' => 'required',
        'txt_email' => 'required|email',
        'txt_phone' => 'required|digits:10'
    ]);

    // Accessing the raw input is generally preferred over Input::get() 
    // in modern Laravel versions, though Input::get() works in older setups.
    $name = $request->input('txt_name');
    $email = $request->input('txt_email');
    $phone = $request->input('txt_phone');

    $type = "bls-training";
    $url = '?' . $request->input('url');

    // Assuming ParseCloud or similar backend logic remains unchanged
    $medicalRequest = new ParseObject("MedicalRequest");
    $medicalRequest->set("name", $name);
    $medicalRequest->set("email", $email);
    $medicalRequest->set("phone", $phone);
    $medicalRequest->set("type", $type);
    $medicalRequest->set("requestMessage", "training");
    $medicalRequest->set("url", $url);

    try {
        $medicalRequest->save();
    } catch (ParseException $ex) {
        // Log the error appropriately instead of echoing to stdout
        \Log::error('ParseException occurred: ' . $ex->getMessage());
    }
}

By switching to digits:10, you tell Laravel to expect a string of exactly 10 characters where every character is a digit. If a user enters "1234567890", validation passes. If they enter "123456789" or "12345678901", the validation fails immediately with a clear error message like "The phone number must be 10 digits."

There is a subtle pitfall here regarding user experience. Phone numbers often include formatting characters like dashes, parentheses, or plus signs (e.g., +1-555-123-4567). The digits:10 rule will reject these immediately because they are not digits. If your business logic requires users to type the number with formatting but then strip it for storage, you must handle that stripping before validation or use a custom validator.

If you strictly require the input to be a raw string of 10 digits without any separators in the form field, digits:10 is the correct tool. However, if you want to allow formatting but validate the underlying length, you would need to normalize the input first using a helper function that strips non-numeric characters before passing it to your validation array or accessing it directly.

Another consideration is the difference between numeric and digits. The numeric rule allows for floats (e.g., 123.45) and negative signs, neither of which are appropriate for a phone number length constraint. Using digits ensures you are counting characters specifically, which aligns with the requirement of exactly 10 digits.

For more complex scenarios where standard rules aren't enough, Laravel allows custom validation rules. You could create a rule that checks if the string is 10 digits long after stripping whitespace and special characters, providing a much smoother user experience for international phone numbers. However, for a strict "exactly 10 digits" requirement without formatting, the native digits rule combined with an exact value is the most performant and readable solution.

If you are looking for deeper insights into validating specific formats or integrating these rules into your broader form handling logic, resources like https://laravelcompany.com can offer additional context on advanced validation strategies within the Laravel ecosystem.

Our network