Hello developers! In this article, we'll see how to validate phone/mobile numbers in laravel 10. We'll validate 10 digit mobile number validation with a regular expression (regex) in laravel 10. We'll use different methods to validate phone numbers such as a digit, size, regular expression, and digit_between.
Laravel provides several 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.
10 digit mobile number validation in laravel 10
We'll create routes, and a controller and add the rule for 10-digit validation in the validate method in the laravel 10 application.
We'll add the following routes in the web.php file.
routes/web.php
use App\Http\Controllers\UserController;
Route::get('/user/create', [UserController::class, 'create']);
Route::post('/user', [UserController::class, 'store']);
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/UserController.php
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Show the form to create a new blog post.
*
* @return \Illuminate\View\View
*/
public function create()
{
return view('users.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.
We'll validate mobile numbers using regular expressions or regex.
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Show the form to create a new blog post.
*
* @return \Illuminate\View\View
*/
public function create()
{
return view('users.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'
]);
}
}
For String length (Exact):
$validated = $request->validate([
'phone_number' => 'required|string|size:10'
]);
For digits length (Exact):
$validated = $request->validate([
'phone_number' => 'required|string|digits:10'
]);
For String Range:
$validated = $request->validate([
'phone_number' => 'required|string|min:6|max:12'
]);
For digits Range:
$validated = $request->validate([
'phone_number' => 'required|string|digits_between:5,12'
]);
You might also like:
- Read Also: How to Validate Form in Laravel 10
- Read Also: Laravel 10 Two Factor Authentication Using Email
- Read Also: 10 Digit Mobile Number Validation in Angular 17
- Read Also: How to use Unique Validation on Update in Laravel 10