0
if (!filter_var($site, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED))...

This validator shows:
https://www.g - as valid address !
www.google.com - as invalid address !

If you type some address into address bar, and you see that it works - it should be a valid address. And vice versa. I need such one validator. Or - I'm missing something in the above code ?

2 Answers 2

4

www.google.com is not a valid url. It needs a protocol for it to be valid i.e. http://www.google.com

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

6 Comments

Musa, I can't expect from user (during registration) that in the You site form input - type http...
@ithcy, yes, I can. But I expect that the validator do that.
@Alegro validators are for validating, not modifying. They only answer the question "is this correct?"
Also please note that http://www.g is a valid URL. It's not a valid internet URL, as g is not a valid TLD, but it is valid.
@Alegro you're going to have to add a default protocol if not is specified then validate the url.
|
3

A user comment on php.net states:

"Please note that FILTER_FLAG_HOST_REQUIRED and FILTER_FLAG_SCHEME_REQUIRED have disappeared. They were previously mentioned in the constants page but it seems that host and scheme are now required by default for the validate_url filter."

Seems like the protocol is a must with recent PHP versions and cannot be disabled.

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.