Laravel 10|9|8 Phone Number Validation Example Tutorial

[ad_1]

Phone/mobile number validation in laravel 10|9|8; In this tutorial, you will learn how to validate unique, min and max mobile or phone number validation in laravel 10|9|8;

Ezoic

In any laravel form, you have phone or mobile number field. At that time, you need to requirement to add phone number validation in laravel 10|9|8 applications. So this tutorial will guide you step by step how to add mobile or phone number validation in laravel using laravel rule and regex. you can easily use with your controller method.

Additionally, you can find 5 more ways to validate phone or mobile number in laravel apps.

10 Digit Mobile/Phone Number Validation in Laravel 10|9|8

Follow the below steps and implement phone or mobile number validation in laravel 10|9|8 apps:

Ezoic

  • Simple Phone or mobile number validation in Laravel
  • Unique mobile number validation in laravel
  • Some addition ways to validate phone numbers in Laravel

Simple Phone or mobile number validation in Laravel

First of all, Navigate to routes folder and open web.php file. Then update the following routes into web.php file, as follow:

        Route::get('/form',[UserController::class, 'index']);
        Route::post('store',[UserController::class, 'store']);

Ezoic

Next, open your command prompt and run the following command:

php artisan make:controller UserController

This command will create controller named UserController.php file.

So navigate to app\Http\Controllers folder and open UserController.php file. Then update the following methods into it, The following code validate a phone or mobile number in laravel apps:

 namespace App\Http\Controllers;    
use Illuminate\Http\Request;
use App\Models\User;
    
class HomeController extends Controller
{
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('user');
    }
    
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
                'name' => 'required',
                'phone' => 'required|digits:10',
                'email' => 'required|email|unique:users'
            ]);
    
        $input = $request->all();
        $user = User::create($input);
      
        return back()->with('success', 'User added successfully.');
    }

Ezoic

Then, Navigate to resources/views and create new blade view file named user.blade.php and update the following code into it:

<!doctype html>
<html lang="en">
  <head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="csrf-token" content="{{ csrf_token() }}">
  <title>Laravel 7 Phone Number Validation Example - Tutsmake.com</title>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" />
</head>
<body>
<div class="container">
    <h2 style="margin-top: 10px;">Laravel 7 Phone Number Validation Example - Tutsmake.com</h2>
    <br>
    <br>
    @if ($message = Session::get('success'))
    <div class="alert alert-success alert-block">
        <button type="button" class="close" data-dismiss="alert">×</button>
          <strong>{{ $message }}</strong>
    </div>
    <br>
    @endif
  
    <form method="post" action="{{url('store')}}">
      @csrf

      <div class="form-group">
        <label for="formGroupExampleInput">Name</label>
        <input type="text" name="name" class="form-control" id="formGroupExampleInput" placeholder="Please enter name">
        <span class="text-danger">{{ $errors->first('name') }}</span>
      </div>

      <div class="form-group">
        <label for="formGroupExampleInput2">Email</label>
        <input type="email" name="email" class="form-control" id="formGroupExampleInput2" placeholder="Please enter password">
        <span class="text-danger">{{ $errors->first('email') }}</span>
      </div>
          
      <div class="form-group">
        <label for="formGroupExampleInput2">Phone Number</label>
        <input type="text" name="phone" class="form-control" id="formGroupExampleInput2" placeholder="Please enter mobile number">
        <span class="text-danger">{{ $errors->first('phone') }}</span>
      </div>

      <div class="form-group">
       <button type="submit" class="btn btn-success">Submit</button>
      </div>
    </form>
</div>
</body>
</html>

Ezoic

Unique mobile number validation in laravel

To validate a unique mobile or phone number in Laravel 10|9|8, you can use the following code for unique validation:

//To validate a unique mobile number in Laravel, you can use the following code
    $this->validate($request, [
      'phone' => 'required|numeric|unique:users',
    ]);

Some additional ways to validate phone numbers in Laravel

Here are some additional ways to validate unique, numeric, min, max phone or mobile numbers in Laravel, you can use following’s:

Ezoic

    //first solution
    $this->validate($request, [
      'phone' => 'required|digits:10'
    ]);
    
   //second solution
    $this->validate($request, [
        'phone' => 'required|numeric|between:9,11'
    ]);
    
   /third solution
    $this->validate($request, [
        'phone' => 'required|min:10|numeric'
    ]);

    //fourth solution
    $this->validate($request, [
        'phone' => 'required|regex:/(01)[0-9]{9}/'
    ]);
   
    //fifth solution
     $this->validate($request, [
        'phone' => 'required|regex:/^([0-9\s\-\+\(\)]*)$/|min:10'
    ]);

Conclusion

That’s it; you have learned how to validate unique, min and max mobile or phone number validation in laravel 10|9|8;

Recommended Tutorials

Ezoic

[ad_2]

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

GSTIN is 03EGRPS4248R1ZD.

Contact
Jassweb, Rai Chak, Punjab, India. 143518
Item added to cart.
0 items - 0.00