Skip to content

Instantly share code, notes, and snippets.

@wchesley
Created May 19, 2023 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wchesley/8787322e4637c8e1548973713c7f230d to your computer and use it in GitHub Desktop.
Save wchesley/8787322e4637c8e1548973713c7f230d to your computer and use it in GitHub Desktop.
Validate IPv4 Address C#
/// <summary>
/// Tests string to see if it is valid IPv4 address
/// </summary>
/// <param name="ip">String to test</param>
/// <returns>True if string is IP address, False otherwise</returns>
private bool ValidateIpv4Address(string ip)
{
IPAddress address;
return ip != null && ip.Count(c => c == '.') == 3 &&
IPAddress.TryParse(ip, out address);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment