Laravel Validation: Don't Use "Numeric" For Phone Numbers

If you have a text input for a phone number, you may want to validate it as a decimal number. Let me show you why it's a bad idea and what to do instead.

I sometimes see validation rules like this:

return [
'phone' => "required|numeric|size:11",
];

It assumes that the phone number is for one local country and is 11 digits long, with a format like this: 37011122222. That is a valid number, so numeric sounds like a good deal?

But it's a good choice only for a few cases.

  • What if your project is international and accepts numbers from different countries?
  • Even if the project is local, would you not accept a tourist visiting your country, to register with a foreign phone number?
  • What if the phone number starts with a "0" digit?
  • What if people want to enter their phone numbers in a different format? With dashes, spaces, and other separator symbols? Same number from the example above: (+370) 111 22222

Ok, I hope I convinced you that numeric is a bad idea. But what is the correct validation rule, then?


The Solution: Laravel Phone Package

There are multiple solutions, and no single "correct" validation exists. Usually, entering a phone number requires both front-end and back-end solutions, with a designer working on the best user experience for the input field.

But on the back end, I would generally advise using a specific package.

One of the most popular ones is this: Propaganistas/Laravel-Phone

It allows you to have a rule phone that you can configure separately.

return [
'phone' => "required|phone",
 
// Or even provide the country/countries:
'mobile_phone' => "required|phone:US,UK",
];

If validation fails, you will get a proper error message like "The :attribute field must be a valid phone number."

You can find many more configuration options in the official package documentation.

avatar

Ok Sound's Good; however I have tried to install and setup the package but there is one thing missing. How to setup the DB $table->string" decimal" or maybe integer" or what the package does not tell me and I have not found anything on the internet that tells us what to use when setting up this package.

$table->string('phone')->nullable(); $table->decimal('phone')->nullable(); $table->intiger('phone')->nullable(); and how about the mobile number? $table->decimal ('mobile')->nullable(); Exe, Need help Please give me some direction as to which is the best way to go when using this package?

PS I wont to use this in a Filament package Can you give me any guidance on that will this work with Filament I hope!.

avatar

This package has https://github.com/Propaganistas/Laravel-Phone?tab=readme-ov-file#database-considerations - which says what types of fields you have to use. It doesn't tell in migration terms, but it's pretty clear.

Not sure if this works with Filament.

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 59 courses (1057 lessons, total 42 h 44 min)
  • 79 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent Premium Tutorials