In this tutorial, we will see how to validate a URL in Laravel. This tutorial URL validation rule will work for every URL like youtube URL, image URL, website URL, etc. Laravel provides a default URL validation helper. But you can use your own regex validation code to validate URLs in Laravel.

In this tutorial, we will both validation rules like Laravel URL validation as well as regex URL validation in Laravel. Let's see the preview image of laravel url validation regex.

larvel-10-url-validation-example

Now to validate the URL in Laravel, update your controller like:

app/Http/Controllers/TutorialController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Validation\Rules\Password;

class TutorialController extends Controller
{
    public function index()
    {
        return view('welcome');
    }

    public function store(Request $request)
    {
        $request->validate([
            'url' => 'required|url'
        ]);
    }
}

 

If you want regex URL validation in Laravel, then follow this code:

app/Http/Controllers/TutorialController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Validation\Rules\Password;

class TutorialController extends Controller
{
    public function index()
    {
        return view('welcome');
    }

    public function store(Request $request)
    {
        $request->validate([
            'url' => ['required','regex:/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i'],
        ]);
    }
}

 

Another way of validating URL in you PHP laravel application with laravel url validation without http like:

app/Http/Controllers/TutorialController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Validation\Rules\Password;

class TutorialController extends Controller
{
    public function index()
    {
        return view('welcome');
    }

    public function store(Request $request)
    {
        $url = strpos(
            $request->url,
            'http'
        ) !== 0 ?
            "http://request->url" : $request->url;

        if (filter_var($url, FILTER_VALIDATE_URL)) {
            return dd('Valid URL');
        } else {
            return dd('Invalid URL');
        }
    }
}

 

Now show the error message in your blade file like:

resources/views/welcome.blade.php

@extends('layouts.app')

@push('style')
@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header" style="background: gray; color:#f1f7fa; font-weight:bold;">
                    Laravel 10 URL Validation Example - Laravelia
                </div>
                 <div class="card-body">                    
                    <form class="w-px-500 p-3 p-md-3" action="{{ route('user.store') }}" method="post" enctype="multipart/form-data">
                        @csrf
                        <div class="row mb-3">
                            <label class="col-sm-3 col-form-label">URL</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control  @error('url') is-invalid @enderror" name="url">
                                @error('url')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>
                        <div class="row mb-3">
                            <label class="col-sm-3 col-form-label"></label>
                            <div class="col-sm-9">
                                <button type="submit" class="btn btn-success btn-block text-white">Submit</button>
                            </div>
                        </div>
                      </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

 

Read also: Laravel 10 Strong Password Validation Example

 

Conclusion

Now we know How to validate a URL in Laravel?. Hope this laravel url validation regex tutorial will help you. After completing this url validation in laravel 10, your concept will be clear about laravel website url validation.