Phone Number Validation in Laravel
In this article, I’m going to show how to validate mobile number in Laravel. Let’s get started:
Table of Contents
Ways of Validation
We ca follow some ways to validate mobile number. Have a look at some ways:
// way 1
$this->validate($request, [
'phone' => 'required|digits:10'
]);
// way 2
$this->validate($request, [
'phone' => 'required|min:10|numeric'
]);
// way 3
$this->validate($request, [
'phone' => 'required|numeric|between:9,11'
]);
// way 4
$this->validate($request, [
'phone' => 'required|regex:/(01)[0-9]{9}/'
]);
// way 5
$this->validate($request, [
'phone' => 'required|regex:/^([0-9\s\-\+\(\)]*)$/|min:10'
]);
Example
Let’s make a simple form:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Phone Number Validation in Laravel</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;">Phone Number Validation in Laravel - MyNotePaper.com</h2>
<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>
Apply validation rules in controller:
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'phone' => 'required|digits:10',
'email' => 'required|email|unique'
]);
$input = $request->all();
$user = User::create($input);
return back()->with('success', 'User added successfully.');
}
We can use any validation ways from the above.
The routes will look like:
Route::get('form','UserController@index');
Route::post('store','UserController@store');
That’s it. Thanks for reading.
Md Obydullah
Software Engineer | Ethical Hacker & Cybersecurity...
Md Obydullah is a software engineer and full stack developer specialist at Laravel, Django, Vue.js, Node.js, Android, Linux Server, and Ethichal Hacking.