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:
1abcd
are allowed (verified on
our local network)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.
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.