Skip to content

Instantly share code, notes, and snippets.

@AdamWhiteHat
Last active January 31, 2021 05:37
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 AdamWhiteHat/f97dd179296c8cc009d0f725f6df3a79 to your computer and use it in GitHub Desktop.
Save AdamWhiteHat/f97dd179296c8cc009d0f725f6df3a79 to your computer and use it in GitHub Desktop.
IsValidIPv6Address
public static bool IPv6Check(string input)
{
if (string.IsNullOrWhiteSpace(input))
{
return false;
}
if (input.IndexOf("::") != input.LastIndexOf("::")) // 'The "::" can only appear once in an address.'
{
return false;
}
string[] parts = input.Split(new char[] { ':' });
int partCount = parts.Length;
if (partCount < 3 || partCount > 8) // From "::" to "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
{
return false;
}
UInt16 intValue = UInt16.MaxValue;
foreach (string part in parts)
{
if (part == "") { continue; } // If you dont want addresses like "::" to be a legal address, remove this line
if (!UInt16.TryParse(part, System.Globalization.NumberStyles.AllowHexSpecifier, System.Globalization.NumberFormatInfo.InvariantInfo, out intValue)) // This ensures that no part has a numeric value greater than (2^16)-1 or FFFF in hexadecimal
{
return false;
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment