Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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