1 |
originally posted 2014-07-22 |
Related: Generate Unique Namespace (in DNS Name Format)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public static bool IsValidDomainName(string name, out string formatMessage) { /* http://en.wikipedia.org/wiki/Domain_Name_System Each label may contain up to 63 characters The characters allowed in a label are a subset of the ASCII character set, and includes the characters a through z, A through Z, digits 0 through 9, and the hyphen. This rule is known as the LDH rule (letters, digits, hyphen). Domain names are interpreted in case-independent manner. Labels may not start or end with a hyphen. */ formatMessage = "Up to 64 letters, digits or hyphens. Cannot start or end with a hyphen."; if (name == null) return false; return Regex.IsMatch(name, @"(^[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]{0,1}$)"); } |