Laravel validation rule — telephone

Image courtesy of UnsplashLaravel validation rule — telephoneMatt KingshottBlockedUnblockFollowFollowingMar 21In 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.

RecapIf 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.

PromotionI’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.


coServer and Site Monitoring with PulseThe check and responseLet’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 worksAs 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 AppRulesTelephoneNumber]]; $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 UpWe 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-rulesI 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!.

. More details

Leave a Reply