How To Validate Youtube Url In laravel Best Practical Example

  • 02-08-2022
  • 877
  • Laravel 9
  • Haresh Chauhan

Youtube link validation rule in laravel; In this post, I will teach you how to validate the youtube link in your form input validation in laravel.

I will make a ruling class. This class will validate the youtube link from the request. This class I will call to validation.

For validation youtube link I will take the help of a regex pattern, I have made an I regex pattern for the youtube link identify, and with Then help of preg_match() PHP function I will match form request youtube link with my regex pattern.

If the preg_match() will check with link to regex patter and matched then it will return true else false. If false means the youtube validation link is invalid. If true then will validation will be false and access next process.

So let's start the valid youtube link validation rule integrate into laravel application with a practical example.

Create Rule Class

Create a ruling class using the below-suggested command in your application. Goto your project and open the terminal just paste this command in your terminal and hit enter.

The "YoutubeValidLink" rule class will create this command in "app/Rules/YoutubeValidLink.php".

php artisan make:rule YoutubeValidLink

Add Rule Validation

Once created rule class in "app/Rules/YoutubeValidLink.php" open the file. We will add `regex_pattern` for youtube link validation in the `passes` method.

In the message method, we will add our custom validation message for the validation response message. The preg_match() PHP function will validate the youtube link with the given regex pattern.


namespace App\Rules\APIV1;

use Illuminate\Contracts\Validation\Rule;

class YoutubeValidLink implements Rule
     * Create a new rule instance.
     * @return void
    public function __construct()

     * Determine if the validation rule passes.
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
    public function passes($attribute, $value)
        $regex_pattern = '/^.*(\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=|\?v=)([^#\&\?]*).*/';
        return preg_match($regex_pattern, $value, $match);

     * Get the validation error message.
     * @return string
    public function message()
        return 'The :attribute is not a valid URL';

Use In Controller

First import the youtube rule class into the controller. With your form youtube URL attribute add an array with this class new YoutubeValidLink.


namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\User;

use App\Rules\YoutubeValidLink; // IMPORT RULE CLASS

class HomeController extends Controller
    public function store(Request $request)
        $validated = $this->validate($request,[
            'youtube_link' => ['required',new YoutubeValidLink]


That's it, If the validation will false, It will return with the given message to the back.

We always thanks to you for reading our blogs.


Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us


Haresh Chauhan


We Are Also Recommending You :