简体   繁体   中英

Laravel validate url name

I need validate url. I need allow only main url sites, example:

http://example.com
https://example.com

I need prevent this urls:

http://example.com/page/blahblahblah
https://example.com/other/bloa

How I can validate only host of url and protocol?

Now I use this:

'url' => 'required|url',

使用此方法,然后检查值以进行验证https://laravel.com/docs/5.2/helpers#method-url ...

Haven't tested this, but the easiest way may be to create a custom validation rule.

php artisan make:rule Maindomain

In the passes() method of app/Rules/Maindomain.php, define the rule. In your case, something like:

return substr_count($value, '/') == 2;

Add a custom error message, if you need one.

Then pass an instance of the rule object like:

$request->validate([
   'url' => [new Maindomain],
]);

I had the same problem so I learnt the regular expressions and created the a class to validate the website url. And so far I was able to solve my problem with this.

Here is the link: How to validate url in laravel using validation rule

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM