Results 1 to 8 of 8

Thread: Need help how to validate string is valid hostname

  1. #1
    Join Date
    Apr 2006
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help how to validate string is valid hostname

    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.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    If you're feeling... er... suicidal... you might want to modify http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Apr 2006
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Holy sh*****t

  4. #4
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    Wow. That must have taken a little while to write.

  5. #5
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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

  6. #6
    Join Date
    Apr 2006
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.)

  7. #7
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by vince144
    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

  8. #8
    Join Date
    Apr 2006
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    thanks again Mike, appreciate the help.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •