I think what you're looking for is something along the lines of:
Code:
function isHostName(string) {
if (/^((2[0-4]|1[0-9]|[1-9])?[0-9]|25[0-5])(\.((2[0-4]|1[0-9]|[1-9])?[0-9]|25[0-5])){3}$/.test(string)) {
var octets = string.split('.');
if ((octets[0] == 0) || (octets[0] == 10) || (octets[0] == 127) || (octets[3] == 255)
|| (octets[3] == 0)
|| ((octets[0] == 169) && (octets[1] == 254))
|| ((octets[0] == 172) && (octets[1] & 0xf0 == 16))
|| ((octets[0] == 192) && (octets[1] == 0) && (octets[2] == 2))
|| ((octets[0] == 192) && (octets[1] == 88) && (octets[3] == 99))
|| ((octets[0] == 192) && (octets[1] == 168))
|| ((octets[0] == 198) && (octets[1] & 0xfe == 18))
|| (octets[0] & 0xf0 == 224)
|| (octets[0] & 0xf0 == 240))
return false;
} else if (!/^([a-z0-9][a-z0-9-]*[a-z0-9]\.)+[a-z]{2,}$/.test(string)) return false;
return true;
}
There are severe limitations:
- The Internet Assigned Numbers Authority is responsible for assigning blocks of IP addresses to regional registries. Though the test above should cover all currently reserved addresses (I make no guarantees), that list may change.
- Not all unreserved IP addresses are in use, therefore whilst an address is valid, it may not exist. The same goes for domain names. There may be no way to ascertain whether this is because of system failure, expected down-time (for example, a machine that is regularly disconnected), or because the address never referred to a real host.
- The domain name validation is very lax. Though it could be strengthened, that would add a large maintenance burden: new top-level and second-level domains may be registered at any time, and these additions would need to be monitored and updated.
Why are you looking to do this? Knowing that might allow for a more practical solution.
Mike
Bookmarks