Very simple function to validate host name string using regular expression. Using this function you can validate any host name.
Regular expression for validating host name
private static readonly Regex validHostnameRegex = new Regex(@"^(([a-z]|[a-z][a-z0-9-]*[a-z0-9]).)*([a-z]|[a-z][a-z0-9-]*[a-z0-9])$", RegexOptions.IgnoreCase);
Function to validate host name
/// <summary> /// Validates a host name. /// </summary> /// <param name="hostname"></param> /// <returns></returns> public static bool IsHostnameValid(string hostname) { if (!string.IsNullOrWhiteSpace(hostname)) { return validHostnameRegex.IsMatch(hostname.Trim()); } return false; }