Search code examples
c#parsingsyntaxip-addressdomain-name

Function that will test for valid IP Address Syntax and Domain Name Syntax


I have a TextBox in a Windows Form that allows a user to enter the Host Address of his device. The user is allowed to enter an IP Address or a Domain Name and I would like to validate that the users' syntax is correct (I'm not concerned whether or not the host address is alive - I'm only concerned about the syntax).

Is there a single .NET (2.0) function (i.e. HostAddress.TryParse()) that will test the syntax of a given host address for an IP and a Domain?


Solution

  • There is nothing that will test both and I don't know about built in host address validation either (unless you are happy with Uri.TryParse and checking that the segments that should be empty are empty).

    You can use IPAddress.TryParse in your own validation function.