12shadow12
12shadow12

Reputation: 317

Validate URL - URL is Valid without TLD?

So, I have an input box where someone can input a URL. When they submit the forum, I would like it to check if it is a valid URL.

Now, my problem is that "http://someurl" (without the TLD) is valid, and "someurl.com" or "www.someurl.com" is not valid (without the http://). Is there a way to fix this?

$input = 'someurl.com'; // Could also be an IP

if (filter_var($input, FILTER_VALIDATE_IP)) {
    echo "IP Valid";
} else if (filter_var($input, FILTER_VALIDATE_URL)) {
    echo "URL Valid";
} else {
    echo "Not an IP or URL";
}

Upvotes: 2

Views: 3377

Answers (1)

DarkDust
DarkDust

Reputation: 92335

The fix is to insert http:// yourself as an URL must have a scheme. This is why the simple www.someurl.com is invalid: it doesn't have a scheme.

URL without top-level domains are perfectly fine, like http://localhost. In a LAN with correctly set search domain you also can use URLs without a domain: for example, if the search domain of your company is somecompany.com and you have the URL http://fileserver, your system will internally build the fully qualified domain name fileserver.somecompany.com. Some URLs don't have hosts at all (for example, tel:), BTW.

Upvotes: 3

Related Questions