Laravel Validation Email or Empty Example

Share Me
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

Today now in this example,I will show you how to use email validation or empty validation in laravel application. So we can easy to use valid email validation in laravel application.

So now in this example, i want to show you how to use email validation also with optional field in laravel application. So now if any user does not want to add email then it will be optional and if he enter something on text box then it check email validation will execute.

Now here is a solution and also here i will give you full example with route and view file too.

Let’s see bellow solution:

$validator = $request->validate([
    'name' => 'required|nullable|string',
    'email' => 'nullable|email',
]);

Route : routes/web.php

Route::get('email-validation','TestController@index');
Route::post('email-validation/store','TestController@store')->name('emailvalidation.store');

Controller : app/Http/Controller/

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class  TestController extends Controller
{
    public function index()
    {
        return view('form');
    }

    public function store(Request $request)
    {
            $input=$request->all();

            $validator = $request->validate([
                'name' => 'required|nullable|string',
                'email' => 'nullable|email',
            ]);

            return redirect()->back();
    }
}	

View : resources/views/form.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>laravel validation email or empty</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha256-L/W5Wfqfa0sdBNIKN9cG6QA5F2qx4qICmU2VgLruv9Y=" crossorigin="anonymous" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha256-WqU1JavFxSAMcLP2WIOI+GB2zWmShMI82mTpLDcqFUg=" crossorigin="anonymous"></script>
</head>
<body style="background: skyblue">
    <div class="container">
        <div class="row">
            <div class="col-md-6 offset-3">
                <div class="card mt-5">
                    <div class="card-header bg-success">
                        <h3 class="text-white text-center"><strong>laravel validation email or empty Codingspoint.com</strong></h3>
                    </div>
                    <div class="card-body">
                        @if(count($errors) > 0)
                            @foreach($errors->all() as $error)
                                <div class="alert alert-danger">{{ $error }}</div>
                            @endforeach
                        @endif
                        <form action="{{ route('emailvalidation.store') }}" method="post">                
                            @csrf
                            <div class="form-group">
                                <label><b>Name :-</b></label>
                                <input type="text" name="name" class="form-control" value="{{ old('password') }}">
                            </div>
                            <div class="form-group">
                                <label><b>Email:-</b></label>
                                <input type="text" name="email" class="form-control" value="{{ old('email') }}">
                            </div>
                            <div class="form-group text-center">
                                <button class="btn btn-success" type="submit">Save</button>
                            </div>
                        </form>     
                    </div>
                </div>      
            </div>
        </div>
    </div>
</body>
</html>

Read Also : Laravel 8 Eloquent Global Scope Tutorial Example

I hope it will help you. Also you can follow us on Facebook

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →