Phone Number Validation in Laravel

Published on July 14, 2020 1 min read

In this article, I’m going to show how to validate mobile number in Laravel. Let’s get started:

Table of Contents

  1. Ways of Validation
  2. Example

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. 🙂

Monthly Newsletter

One email a month, packed with the latest tutorials, delivered straight to your inbox.
We'll never send any spam or promotional emails.
Author

Hey, I'm Md Obydullah. I build open-source projects and write on Laravel, Linux server, modern JavaScript and more on web development. If you enjoy my content, please consider supporting what I do!

Follow Buy me a coffeeBuy me a coffee

Leave a Reply

Your email address will not be published. Required fields are marked *