Laravel Validation for URL Example

Share Me
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share
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

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 →