Last active
January 31, 2021 05:37
-
-
Save AdamWhiteHat/f97dd179296c8cc009d0f725f6df3a79 to your computer and use it in GitHub Desktop.
IsValidIPv6Address
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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