In our QuickAdMinpanel generator we don’t have a specific ‘phone’ field type, but you can easily put the phone number validation to a ‘text’ field type, this tutorial will show you how.

We’ll use a propaganistas/laravel-telephone package, which is really popular and has almost 1,000 GitHub stars.

Imagine we have this shape:

What if we want to add a validation rule to accept only UK phone numbers? Easy.

Step 1. Install the package.


composer require propaganistas/laravel-phone

Step 2. Add validation error message.

The plan won’t do this for you, so you have to do it manually. Go to Resources / long / en / validation.php (Replace β€œen” with your language, if you have another) and add this key value:


    'phone' => 'The :attribute field contains an invalid number.',

Step 3. Add a validation rule to form the request file.

We use form requests to manage validation rules, so all you have to do is use another rule there:


return [
    'address' => 'required',
    'phone' => 'phone:GB'
];

Ruler Telephone: xx comes from this package, and the 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.



Technology

Another Tech Information

Similar Posts