Laravel validation rule — telephone

mattkingshott profile image Matt Kingshott 👨🏻‍💻 Originally published at Medium on ・3 min read

Image courtesy of Unsplash

Laravel validation rule — telephone

In this new series, we’ll be exploring the concept of custom validation rules in Laravel and why you’ll want to use them. I’m aiming to release a new article each day containing a new rule which you can use in your own projects. I’ll also be collecting these rules into a package that you can pull in via composer.


If you’re unfamiliar with the reasoning behind creating custom rules to handle data validation, check out the intro section from the first article in this series.

You may also want to review the section on creating a rule (in the same post), as we won’t be repeating the mechanics of each method in your validation classes, we’ll just be exploring the code required to make the rule work.


I’ve recently released Pulse — a friendly, affordable server and site monitoring service designed specifically for developers. It includes all the usual hardware monitors, custom service monitors, alerting via various notification channels, logs, as well as full API support. Take a look… https://pulse.alphametric.co

Server and Site Monitoring with Pulse

The check and response

Let’s begin by writing the logic necessary to ensure that the user has provided a valid phone number. Per current conventions, a phone number is valid if it is between 7 and 15 characters long and only includes integers.

As with the strong password rule we created, the easiest way to enforce each of these requirements, is to use a regular expression:

public function passes($attribute, $value)
    return preg\_match("/^[0-9]{7,15}$/", $value);

Next, we’ll need to write an error message to return when the user has not supplied a valid phone number:

public function message()
    return 'The :attribute must be a valid telephone number (7 - 15 digits in length)';

Testing it works

As before, we’ll write a quick unit test to confirm that the regular expression works correctly and rejects invalid phone numbers:

/\*\* @test \*/
public function a\_telephone\_number\_can\_be\_validated()
    $rule = ['phone' => [new App\Rules\TelephoneNumber]];

    $this->assertFalse(validator(['phone' => '1'], $rule)->passes());

    $this->assertFalse(validator(['phone' => '1#'], $rule)->passes());

    $this->assertFalse(validator(['phone' => 'a1#'], $rule)->passes());

    $this->assertFalse(validator(['phone' => 'aB1#'], $rule)->passes());

    $this->assertFalse(validator(['phone' => '123456'], $rule)->passes());

    $this->assertFalse(validator(['phone' => '1234567890123456'], $rule)->passes());

    $this->assertTrue(validator(['phone' => '123456789'], $rule)->passes());


Wrapping Up

We now have a reusable validation rule to ensure that users supply a valid telephone number when we require it. We also respond with a suitable error message when the check fails, and we have a test to ensure the rule works.

You can see the complete class and pull it into your projects by visiting the repo on Github: https://github.com/alphametric/laravel-validation-rules

I have additional validation rules that I intend to share with you in the coming days, so be sure to follow me for those articles. If you’re interested, you can also follow me on Twitter to see everything I’m up to.

Happy coding!

Posted on Apr 8 '19 by:

mattkingshott profile

Matt Kingshott 👨🏻‍💻


Founder. Developer. Writer. Lunatic. Created Pulse, IodineJS, Axiom, and more. #PHP #Laravel #Vue #TailwindCSS


markdown guide