John Davidson

php - How to validate a custom URL in laravel with protocols specified?

0 comments
Message:


I want to validate the URL with the specified protocol.


HTTP and HTTPS type validation is not applicable for the below URLs.



  1. If the URL is secure the see below.


    rtmps://username:password@server:port/



  2. Without a secured URL will be as follows.


    rtmp://server:port/




I only need validation for the above 2 URL(s) only.


I have tried laravel's URL type validation but it is not validating for these protocols.


Also want to tell you only I am able to use is regex validation, because it is a request class of laravel where I have put the validation rule.
laravel version 6.2



My Answer:
You can use a regex validation rule in Laravel to validate custom URLs with specified protocols. Here is an example of how you can achieve this:

php
$request->validate([
'url' => ['required', 'regex:/^(rtmps:\/\/[a-zA-Z0-9]+:[a-zA-Z0-9]+@[a-zA-Z0-9]+:[0-9]+\/)|(rtmp:\/\/[a-zA-Z0-9]+:[0-9]+\/)$/']
]);


In this example, the regex pattern validates URLs with the specified protocols 'rtmps://' and 'rtmp://'. The pattern checks for the format of the URLs you provided, including the username, password, server, and port.

You can adjust the regex pattern as needed to match the specific format of the URLs you want to validate.

Rate this post

5 of 5 based on 5155 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us