PDA

View Full Version : Need help how to validate string is valid hostname



vince144
09-14-2006, 08:32 PM
I'm fairly new to javascript, so pardon my inexperience does anyone know how to validate that a string enter in a form is a valid hostname for all platforms, I guess the string could also be an ipdress as well so it would have to validate that first.

Any help appreciated thanks.

Vince.

Twey
09-14-2006, 08:49 PM
If you're feeling... er... suicidal... you might want to modify http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html.

vince144
09-14-2006, 08:55 PM
Holy sh*****t

blm126
09-14-2006, 09:39 PM
Wow. That must have taken a little while to write.

mwinter
09-15-2006, 12:12 AM
I think what you're looking for is something along the lines of:



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

vince144
09-15-2006, 06:47 PM
My form asks the user to enter the server name and I wanted to validate the string to ensure that its a valid ipadress or that the hostname is valid. (I don't care if the machine is up or not)

BTW thanks for the code snippet (would it statisfy the above criteria.)

mwinter
09-17-2006, 07:37 PM
My form asks the user to enter the server name and I wanted to validate the string to ensure that its a valid ipadress or that the hostname is valid. (I don't care if the machine is up or not)

Naturally you'd want to perform this test on the server, too.



BTW thanks for the code snippet (would it statisfy the above criteria.)

It would provide a reasonable sanity check, but the only way to be sure is to contact the server.

Mike

vince144
09-19-2006, 07:06 PM
thanks again Mike, appreciate the help.