Laravel URL Validation Rule Example

By Hardik Savani | July 15, 2020 | Category : Laravel


In this article we will cover on how to implement laravel validation for url example. you can see laravel url validation rule example. you can see laravel validation rule url. This article will give you simple example of how to add url validation in laravel. Let's get started with laravel input link validation example.

you can use url validation rule with laravel 6, laravel 7 and laravel 8 application.

we will use url validation rule for allow only url in laravel 7 and laravel 6. i will give you two example with allow https, http, www links using url rule and regex rule.

we may sometime requirement to add validation for url in our laravel application. so i will show how to validation for url in laravel 7 using laravel alpha. you can easily use with your controller method.

I will give you way to add validation in laravel. so i will just show you controller code and preview here. so you can also follow form validation with laravel with this code: Laravel Form Validation Example.

Example 1:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

use Illuminate\Validation\Rule;

class StoreUser extends FormRequest

{

/**

* Determine if the user is authorized to make this request.

*

* @return bool

*/

public function authorize()

{

return true;

}

/**

* Get the validation rules that apply to the request.

*

* @return array

*/

public function rules()

{

return [

'name' => 'required',

'url' => 'required|url',

'email' => ['required', Rule::unique('users')]

];

}

}

Example 2:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

use Illuminate\Validation\Rule;

class StoreUser extends FormRequest

{

/**

* Determine if the user is authorized to make this request.

*

* @return bool

*/

public function authorize()

{

return true;

}

/**

* Get the validation rules that apply to the request.

*

* @return array

*/

public function rules()

{

return [

'name' => 'required',

'url' => ['required','regex:/\b(?:(?:https?|ftp):\/\/|www\.)[[email protected]#\/%?=~_|!:,.;]*[[email protected]#\/%=~_|]/i'],

'email' => ['required', Rule::unique('users')]

];

}

}

I hope it can help you...




Hardik Savani
My name is Hardik Savani. I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.
Follow Me: Github Twitter
***Do you want me hire for your Project Work? Then Contact US.

We are Recommending you: