Home php Laravel Validation for URL Example

Laravel Validation for URL Example

by Shahriar Sagor
Laravel Validation for URL Example

Hi Dev’s, Today now in this blog, I will give you a example of how to cheek validation a url in laravel application. Now in this validation we will check url is valid or not. If It will returns false, then it will be saying “The url is not a valid URL”.

Now i want to validate urls in laravel application. Laravel give us a very simple and easy way to create a url validation. So now I will give you a very short and simple way to url validation you can understand very well. Now you have to just need to following bellow example.

So if you will use regex on url in laravel then you can 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
}

Read Also : Laravel Ajax Image Upload With Intervention Package

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

close

You may also like