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','[email protected]');
Route::post('store','[email protected]');
That’s it. Thanks for reading. ?
Comment
Preview may take a few seconds to load.
Markdown Basics
Below you will find some common used markdown syntax. For a deeper dive in Markdown check out this Cheat Sheet
Bold & Italic
Italics *asterisks*
Bold **double asterisks**
Code
Inline Code
`backtick`Code Block```
Three back ticks and then enter your code blocks here.
```
Headers
# This is a Heading 1
## This is a Heading 2
### This is a Heading 3
Quotes
> type a greater than sign and start typing your quote.
Links
You can add links by adding text inside of [] and the link inside of (), like so:
Lists
To add a numbered list you can simply start with a number and a ., like so:
1. The first item in my list
For an unordered list, you can add a dash -, like so:
- The start of my list
Images
You can add images by selecting the image icon, which will upload and add an image to the editor, or you can manually add the image by adding an exclamation !, followed by the alt text inside of [], and the image URL inside of (), like so:
Dividers
To add a divider you can add three dashes or three asterisks:
--- or ***

Comments (0)