Laravel returns true check for invalid urls

In laravel, I want to check a simple url, for example:

http://www.google.com
http://google.com
www.google.com
http://www.google.com
http://www.google

Unfortunately, laravel verification code is like

['redirect_url' => 'required|url']

return falsefor these URLs:

www.google.com

and return true for the address http://www.google, how to check URLs in laravel correctly, this check is not the best check for URLs

+4
source share
3 answers

You can use regex URL validation rule

 ['redirect_url' => ['required','regex:/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i']]
+3
source

Before checking, check if the string contains http://or https://using this code:

if(!(starts_with($url, "http://") || starts_with($url, "https://")) {
        $url = "http://".$url;
}

starts_with() Laravel. docs

+1

regex, Jay Dhameliya , , , , , URL , ,

// validateURL , URL- .

  • (http/https/etc)
  • auth
  • IP-
  • / / -

There seems to be no alternatives provided for this validator method or ways to say that this is not required. So again, a custom regex seems like the best option. If you plan to use it in more than one place, you might consider creating your own custom validation rule to see docs ..

0
source

Source: https://habr.com/ru/post/1620079/


All Articles