3

Looking for hostname validation regex.

Regular expression to match DNS hostname or IP Address?

In that link gentlemen propose a decent regex. I have a few problems/questions with that:

  • On windows computers/networks names like 1abcd are allowed (verified on our local network)
  • In the proposed regex the dot might appear only once. I'd assume that abc.def.gh is a valid hostname as well, isn't it.

Strange, but also couldn't find any .NET class that can validate hostname string (is it the situation?). Any advice will be highly appreciated.

Update: for any class/method proposal - please advice something that will work both in .NET/C# and SilverLight.

Community
  • 1
  • 1
BreakPhreak
  • 10,940
  • 26
  • 72
  • 108

3 Answers3

3

Is Uri.CheckHostName a method that can help you ? If the hostname is not recognize, the result will be "Unknown".

kerrubin
  • 1,606
  • 1
  • 20
  • 25
  • That method name looked promising to me till today morning. However, names like "9999999" pass (should they?). Also names like "1.3" pass as well (should they?). – BreakPhreak Jan 10 '11 at 09:26
  • 1
    It seems to be valid hostname, according to what I understand in [wikipedia](http://en.wikipedia.org/wiki/Hostname#Restrictions_on_valid_host_names) – kerrubin Jan 10 '11 at 09:33
  • The method doesn't work in Silverlight and the code is supposed to serve SL platform as well. – BreakPhreak Jan 10 '11 at 12:41
  • I'd see [Dns.GetHostEntry](http://msdn.microsoft.com/en-us/library/system.net.dns.aspx) in Silverlight (but never use). It resolve the hostname and launch ArgumentException if it's not a valid hostname. On the right there is an other question [validate hostname string in Python](http://stackoverflow.com/questions/2532053/validate-hostname-string-in-python) with this regex "(?!-)[A-Z\d-]{1,63}(?<!-)$" – kerrubin Jan 10 '11 at 13:27
3

In the proposed regex the dot might appear only once. I'd assume that abc.def.gh is a valid hostname as well, isn't it.

The dot MAY appear more than once. Test the regular expression here and you will see that it matches.

Relevant fragment of the regular expression (first part is):

([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*

On windows computers/networks names like 1abcd are allowed (verified on our local network)

From wikipedia:

The original specification of hostnames in RFC 952, mandated that labels could not start with a digit or with a hyphen, and must not end with a hyphen. However, a subsequent specification (RFC 1123) permitted hostname labels to start with digits.

I referred to RFC 952. I will try to update the regular expression for hostnames to be compliant with RFC 1123.

Community
  • 1
  • 1
Jorge Ferreira
  • 96,051
  • 25
  • 122
  • 132
  • 1
    Another thing to consider is that segments may be no more than 63 characters in length and the entire hostname cannot exceed 253 characters in length. – Bluebaron Aug 08 '15 at 18:03
0

(?i)(?=.{5,20}$)^(([a-z\d]|[a-z\d][a-z\d\-]*[a-z\d])\.)*([a-z\d]|[a-z\d][a-z\d\-]*[a-z\d])$

Explanation:

(?i) - Ignore case

(?=.{5,20}$) - Limit the string length within 5 to 20 characters.

^(([a-z\d]|[a-z\d][a-z\d\-]*[a-z\d])\.)*([a-z\d]|[a-z\d][a-z\d\-]*[a-z\d])$ - Accepts any alphabets and numeric along with hyphen and dot

Borys Verebskyi
  • 4,160
  • 6
  • 28
  • 42