We can statically validate a phone or mobile number in Laravel using regex. But it will work for a specific country. So in this tutorial, we will validate the phone numbers in Laravel 10 for all countries using country code.

There is a beautiful package for validation phone numbers and the package name is "propaganistas/laravel-phone". Using this package we can easily validate phone number validation with country code in laravel 10. This laravel phone number validation package is good and it has two thousand plus git stars.

Let's see the error message of validation error if someone inputs an invalid phone number:

laravel-10-phone-number-validation-example-with-country-code

So now install this package:

composer require propaganistas/laravel-phone

 

Now update the validation.php file to show the exact error message:

lang/en/validation.php

<?php

return [
    'phone' => 'The :attribute field must be a valid number.',
];

 

Now to validate laravel 10 phone number validation, use this code like below:

app/Http/Controllers/TutorialController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TutorialController extends Controller
{
    public function index()
    {
        return view('welcome');
    }

    public function store(Request $request)
    {
        $request->validate([
            'phone' => 'phone:US,BD'
        ]);

        return $request;
    }
}

 

Now show the error message in your blade file like:

resources/views/welcome.blade.php

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header" style="background: gray; color:#f1f7fa; font-weight:bold;">
                    Laravel 10 Phone Number Validation Example - Laravelia
                </div>
                 <div class="card-body">                    
                    <form 
                        action="{{ route('user.store') }}" 
                        method="post" 
                        enctype="multipart/form-data"
                    >
                    @csrf
                        <div class="row mb-3">
                            <label class="col-sm-3 col-form-label">
                                Phone
                            </label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control  @error('phone') is-invalid @enderror" name="phone">
                                @error('phone')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>
                        <div class="row mb-3">
                            <label class="col-sm-3 col-form-label"></label>
                            <div class="col-sm-9">
                                <button type="submit" class="btn btn-success btn-block text-white">Submit</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

 

Read also: Laravel 10 Validate Any Type Of File Example

 

Conclusion

Now we know laravel phone number validation with country code. Hope this laravel phone number validation regex tutorial will help you. After completing laravel phone number validation package, your concept will be clear about laravel 10 phone number validation.