Laravel 10|9|8 Phone Number Validation Example Tutorial

Laravel 10|9|8 Phone Number Validation Example Tutorial

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

In any laravel form, you have phone or mobile number field, At that time, you need to add phone number validation in laravel 11, 10, 9, and 8 applications. So this tutorial guide, we will show you step by step how to add mobile or phone number validation in laravel using regex rules.

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

Here are some ways to validate (digit, unique, min, and max) phone or mobile number in laravel 11, 10, 9, 8 apps using regex, digit, and unique validation rules:

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']);

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.');
    }

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 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>

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:

    //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 11, 10, 9 and 8.

Recommended Tutorials

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *