In this tutorial you will learn about the Laravel 7 Phone Number Validation Example and its application with practical example.
In this Laravel 7 Phone Number Validation Example tutorial I’ll show you how to create custom phone number validation in laravel. In this tutorial you will learn to create phone number validation in laravel. In step by step tutorial I’ll demonstrate you to implement custom phone number validation in laravel.
Laravel 7 Phone Number Validation Example
Step 1: Add Routes
In this step we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 |
Route::get('form','UserController@index'); Route::post('store','UserController@store'); |
Step 2: Create Controller & Method
Now, lets create a controller named UserController using command given below –
1 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
namespace App\Http\Controllers; use Illuminate\Http\Request; use App\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.'); } |
Step 3: Create Blade View
Now, Navigate to resources/views and create new blade view file named user.blade.php and update the following code into it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<!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</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</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> |
The below also provide a different way for laravel phone number validation methods, you can also use instead of the above methods:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
//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' ]); |