How to Use Date Format Validation in Laravel?

By Hardik Savani April 16, 2024 Category : Laravel

Hi artisan,

As we know laravel 6 provide date validation like date after, date_format, after_or_equal:date, before:date, before_or_equal:date etc. so in this example, i will show you how to use validation date after or equal today in laravel, how to use date_format validation in laravel, laravel validation date before or equal today etc.

you can add date format validation in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 project.

You can see i listed following date validation that provided by laravel by default:

1) date

2) date_format

3) after:date

4) after_or_equal:date

5) before:date

6) before_or_equal:date

Now we will show one by one with example so, let's see bellow controller example:

date Validation:

public function store(Request $request)

{

$request->validate([

'name' => 'required',

'email' => 'required|email',

'birth_date' => 'date'

]);

}

date_format Validation:

public function store(Request $request)

{

$request->validate([

'name' => 'required',

'email' => 'required|email',

'birth_date' => 'date_format:m/d/Y'

]);

}

after Validation:

public function store(Request $request)

{

$request->validate([

'name' => 'required',

'email' => 'required|email',

'start_date' => 'date_format:m/d/Y|after:tomorrow'

]);

}

after_or_equal Validation:

public function store(Request $request)

{

$todayDate = date('m/d/Y');

$request->validate([

'name' => 'required',

'email' => 'required|email',

'start_date' => 'date_format:m/d/Y|after_or_equal:'.$todayDate

]);

}

before Validation:

public function store(Request $request)

{

$request->validate([

'name' => 'required',

'email' => 'required|email',

'end_date' => 'date_format:m/d/Y|before:start_date',

'start_date' => 'date_format:m/d/Y|after:tomorrow'

]);

}

before_or_equal Validation:

public function store(Request $request)

{

$request->validate([

'name' => 'required',

'email' => 'required|email',

'end_date' => 'date_format:m/d/Y|before_or_equal:start_date',

'start_date' => 'date_format:m/d/Y|after:tomorrow'

]);

}

Most of we use start date and end date validation as like bellow i written example:

Start Date and End Date Example:

public function store(Request $request)

{

$request->validate([

'name' => 'required',

'email' => 'required|email',

'start_date' => 'date_format:m/d/Y',

'end_date' => 'date_format:m/d/Y|after:start_date'

]);

}

I hope it can help you...

Shares