Laravel 9 Phone Number Validation Using Regex

Websolutionstuff | Oct-25-2022 | Categories : Laravel PHP

In this article, we will see laravel 9 phone number validation using regex. In laravel 9 we will see different methods of validating phone numbers. Also, we will see an example of 10-digit phone number validation in laravel 9 and mobile number validation using regex or regular expression.

So, let's see how to validate phone numbers in laravel 9 and phone number validation regex in laravel 9.

Laravel provides several different 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 9.

Example 1: How to Validate Mobile Numbers / Phone Numbers in Laravel 8/9

 In this example, we will create routes, and a controller and add the rule for 10-digit validation in validate method in the laravel 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\PostController;
 
Route::get('/post/create', [PostController::class, 'create']);
Route::post('/post', [PostController::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/PostController.php

<?php
 
namespace App\Http\Controllers;
 
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
 
class PostController extends Controller
{
    /**
     * Show the form to create a new blog post.
     *
     * @return \Illuminate\View\View
     */
    public function create()
    {
        return view('post.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: Mobile Number Validation Using Regex In Laravel

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 PostController extends Controller
{
    /**
     * Show the form to create a new blog post.
     *
     * @return \Illuminate\View\View
     */
    public function create()
    {
        return view('post.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'
        ]);
    }
}

 


You might also like:

Recommended Post
Featured Post
How To Change Text In Laravel 9 Datatable
How To Change Text In Laravel...

Do you want to learn how to change text in a Laravel 9 Datatable? This blog post is your complete guide to mastering tex...

Read More

Jan-06-2023

How to Install PHP Soap Extension in Ubuntu 23.04
How to Install PHP Soap Extens...

Hey fellow developers! Today, let's tackle the installation of the PHP SOAP extension on our Ubuntu 23.04 systems. I...

Read More

Jan-31-2024

Access Denied for user 'root'@'localhost' phpMyAdmin
Access Denied for user 'root'@...

phpMyAdmin is a valuable web-based tool for managing MySQL databases, simplifying various database tasks. However, runni...

Read More

Jul-28-2023

How to Create Login and Register in Node.js
How to Create Login and Regist...

As I embarked on my journey to develop a powerful web application, I realized the importance of a robust user authentica...

Read More

Oct-02-2023