How To Validate Phone Number In Laravel 10

Websolutionstuff | May-10-2023 | Categories : Laravel PHP

In this article, we will see how to validate phone numbers in laravel 10. Here, we will learn about mobile number validation in laravel 10. There are different methods of validating phone numbers. we will see an example of 10-digit phone number validation in laravel 10.

So, let's see laravel 10 validates the phone numbers, 10-digit mobile number validation in laravel 10, laravel 10 phone number validation using Regex, and mobile number validation in laravel 10.

Laravel provides several methods to validate your application's incoming data. It is most common to use the validate method available on all incoming HTTP requests. The validation rules are passed into the validate method.

Learn more about laravel validation: Official Document of Laravel 10.

Example 1: Validate Mobile Numbers / Phone Numbers

 In this example, we will create routes, and a controller and add the rule for 10-digit validation in validate method in the laravel 10 application.

 

Step 1: Add Route

In this step, we will add the following routes in the web.php file.

routes/web.php

use App\Http\Controllers\UserController;
 
Route::get('/user/create', [UserController::class, 'create']);
Route::post('/user', [UserController::class, 'store']);

 

Step 2: Create Controller

Now, we will create two functions and add the following code to the controller file. In the store function, we will add validation that must have an exact length of the value. Also, you can use min or max length validation as per requirements.

app/Http/Controllers/UserController.php

<?php
 
namespace App\Http\Controllers;
 
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
 
class UserController extends Controller
{
    /**
     * Show the form to create a new blog post.
     *
     * @return \Illuminate\View\View
     */
    public function create()
    {
        return view('users.create');
    }
 
    /**
     * Store a new blog post.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validated = $request->validate([
            'phone_number' => 'required|numeric|digits:10'
        ]);
    }
}

The validation rules are passed into the validate method. all available validation rules are documented.

  • If the validation rules pass, your code will keep executing normally;
  • however, if validation fails, an Illuminate\Validation\ValidationException exception will be thrown and the proper error response will automatically be sent back to the user.

 

 

Example 2: Phone Number Validation Using Regex In Laravel 10

In this example, we will validate mobile numbers using regular expressions or regex.

<?php
 
namespace App\Http\Controllers;
 
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
 
class UserController extends Controller
{
    /**
     * Show the form to create a new blog post.
     *
     * @return \Illuminate\View\View
     */
    public function create()
    {
        return view('users.create');
    }
 
    /**
     * Store a new blog post.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validated = $request->validate([
            'phone_number' => 'required|regex:/^([0-9\s\-\+\(\)]*)$/|min:9'
        ]);
    }
}

 

Example 3:

For String length (Exact):

$validated = $request->validate([
   'phone_number' => 'required|string|size:10'
]);

For digits length (Exact):

$validated = $request->validate([
   'phone_number' => 'required|string|digits:10'
]);

For String Range:

$validated = $request->validate([
   'phone_number' => 'required|string|min:6|max:12'
]);

For digits Range:

$validated = $request->validate([
   'phone_number' => 'required|string|digits_between:5,12'
]);

 


You might also like:

Recommended Post
Featured Post
How To Change Table Name Using Laravel 10 Migration
How To Change Table Name Using...

In this article, we will see how to change the table name using laravel 10 migration. Here, we will learn about the...

Read More

Apr-28-2023

Laravel 8 Inner Join Query Example
Laravel 8 Inner Join Query Exa...

In this tutorial we will learn about laravel 8 inner join query example. Also see how to join two tables in laravel 8. I...

Read More

Nov-24-2021

Laravel 9 Livewire Datatable Example
Laravel 9 Livewire Datatable E...

In this article, we will see the laravel 9 livewire datatable example. Here, we will learn how to use livewire data...

Read More

Nov-30-2022

How To Use Sweetalert2 In Laravel
How To Use Sweetalert2 In Lara...

Today we will learn how to use sweetalert2 In laravel, You can use sweetalert2 in laravel as well as php, ...

Read More

May-03-2021