Phone Field Validation with Laravel-Phone Package
In our QuickAdminPanel generator, we don’t have specific “phone” field type, but you can put phone number validation easily to any “text” field type, this tutorial will show you how.
We will use a package Propaganistas/Laravel-Phone, which is really popular and has almost 1,000 Github stars.
Imagine we have this form:
What if we want to add a validation rule to accept only phone numbers from UK? Easy.
Step 1. Install the package.
composer require propaganistas/laravel-phone
Step 2. Add validation error message.
Package won’t do that for you, so you need to do this manually. Go to resources/lang/en/validation.php (replace “en” with your language, if you have another one) and add this key-value:
'phone' => 'The :attribute field contains an invalid number.',
Step 3. Add validation rule to Form Request file.
We use Form Requests to handle validation rules, so all you need to do is use another rule there:
return [ 'address' => 'required', 'phone' => 'phone:GB' ];
Rule phone:XX comes from this package, and country name should be one of the official country codes (so it’s not UK, it’s GB for Great Britain).
And that’s it, invalid phone number will give you an error!
There are more features in the package, check the official documentation.
Try our QuickAdminPanel Generator!
How it works:
1. Generate panel online
No coding required, you just choose menu items.
2. Download code & install locally
Install with simple "composer install" and "php artisan migrate".
3. Customize anything!
We give all the code, so you can change anything after download.