Laravel Validation for URL Example

Admin   Laravel   703  2021-03-17 03:00:05

Hi Dev,

In this blog, I will show you how to validate on url in laravel. This validation in check url is valid or not. It returns false, saying "The url is not a valid URL".

I want to validate urls in laravel. Laravel provide very simple way to create url validation. I give you very simple way to url validation you can understand very well. So you have to just following bellow example.

If you will use regex on url in laravel then use second example.

Example 1

 

/**

* The attributes that are mass assignable.

*

* @var array

*/

public function store(Request $request)

{

$input = $request->all();

$request->validate([

'url' => 'required|url'

]);

// Url Validation

}

Example 2

 

/**

* The attributes that are mass assignable.

*

* @var array

*/

public function store(Request $request)

{

$input = $request->all();

$regex = '/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/';

$request->validate([

'url' => 'required|regex:'.$regex,

]);

// Url Validation using regex

}

It will help you...