0

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',
4
  • 1
    based on your example counting the number of "/" is all you need Commented Jan 14, 2019 at 20:30
  • A regex rule could accomplish what you need, like 'url' => 'required|regex:...', but I'm not a regex expert, so I'm not sure what the pattern would look like. Commented Jan 14, 2019 at 20:33
  • I agree with @tim, though you can also use in:url1,url2. Commented Jan 14, 2019 at 20:50
  • Download regexbuddy or generate regex for your required case and append it like this in your validation '"url" => "required|regex:".$regex' Commented Jan 14, 2019 at 20:51

3 Answers 3

0

use this methods, then check value for validation https://laravel.com/docs/5.2/helpers#method-url ...

Sign up to request clarification or add additional context in comments.

Comments

0

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],
]);

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.