Friday, January 31, 2025
Google search engine
HomeLanguagesLaravel 7 Phone Number Validation Example

Laravel 7 Phone Number Validation Example

Laravel Validation for phone number example. In this tutorial, you will learn how to use validation for phone number in laravel apps.

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 application. so this tutorial will guide you step by step how to add mobile validation in laravel using laravel rule and regex. you can easily use with your controller method.

At last of tutorial, you will see 5 more way to validate phone number in laravel apps.

Laravel Phone Number Validation

Follow the below steps and implement phone number validation in laravel apps:

  • Add Routes
  • Create Controller
  • Create Blade View

Step 1: Add Routes

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@index');
Route::post('store','UserController@store');

Step 2: Create Controller & Method

Now, 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:

    
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:

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

The below also provide a different way for laravel phone number validation methods, you can also use instead of the above methods:

    //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'
    ]);
Dominic Wardslaus
Dominic Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments