;

How to Validate Whether an IPv6 IP Address is Valid or Not in C#


Tutorialsrack 31/07/2020 C#

In this article, you will learn how to check whether an IPv6 IP address is valid or not in C#. There are various ways to validate whether an IPv6 IP address is valid or not. 

The following list shows examples of valid IPv6 (Normal) addresses:

  • 2001 : db8: 3333 : 4444 : 5555 : 6666 : 7777 : 8888
  • 2001 : db8 : 3333 : 4444 : CCCC : DDDD : EEEE : FFFF
  • : : (implies all 8 segments are zero)
  • 2001: db8: : (implies that the last six segments are zero)
  • : : 1234 : 5678 (implies that the first six segments are zero)
  • 2001 : db8: : 1234 : 5678 (implies that the middle four segments are zero)
  • 2001:0db8:0001:0000:0000:0ab9:C0A8:0102 (This can be compressed to eliminate leading zeros, as follows: 2001:db8:1::ab9:C0A8:102 )

Here are some Example of programs to validate the IPv6 IPAddress is valid or Not in C#.

Example 1: Using IPAddress.TryParse() Method

In this example, first, we need to import these two Namespaces System.Net and

System.Net.Sockets and then, we used the IPAddress.TryParse() method.

Here is the source code of the Program to validate whether the IPv6 IP Address is valid or Not.

Example 1: Using IPAddress.TryParse() Method
using System;
using System.Net;
using System.Net.Sockets;

namespace Tutorialsrack
{
    class Program
    {
        /* How to Validate the IPv6 IP Address is Valid or Not in C# */
        static void Main(string[] args)
        {
            IsValid_IPv6_IPAddress("2001:0db8:85a3:0000:0000:8a2e:0370:7334");
            IsValid_IPv6_IPAddress("2001:0db8:85a3:0000:0000:8a2e:0370");
            IsValid_IPv6_IPAddress("2001:0db8:85a3:0000:0000:8a2e::5");
            IsValid_IPv6_IPAddress("2001:0db8:85a3:0000:0000::2:7335");
            IsValid_IPv6_IPAddress("::1");
            IsValid_IPv6_IPAddress("::");
            IsValid_IPv6_IPAddress("::1234:5678");
            IsValid_IPv6_IPAddress("2001:db8::");
            IsValid_IPv6_IPAddress("0.0.0.5");
            IsValid_IPv6_IPAddress("5");
            IsValid_IPv6_IPAddress("Foo");
            IsValid_IPv6_IPAddress("1::5");

            //Hit ENTER to exit the program
            Console.ReadKey();
        }

        public static void IsValid_IPv6_IPAddress(string IpAddress)
        {
            try
            {
                bool flag = false;
                if (!string.IsNullOrWhiteSpace(IpAddress))
                {
                    IPAddress ip;
                    if (IPAddress.TryParse(IpAddress, out ip))
                    {
                        flag = ip.AddressFamily == AddressFamily.InterNetworkV6;
                    }
                }
                if (flag)
                {
                    Console.WriteLine("{0} is a valid IPv6 IP address", IpAddress);
                }
                else
                {
                    Console.WriteLine("{0} is not a valid IPv6 IP address", IpAddress);
                }
            }
            catch (Exception) { }
        }
    }
}
Output

2001:0db8:85a3:0000:0000:8a2e:0370:7334 is a valid IPv6 IP address

2001:0db8:85a3:0000:0000:8a2e:0370 is not a valid IPv6 IP address

2001:0db8:85a3:0000:0000:8a2e::5 is a valid IPv6 IP address

2001:0db8:85a3:0000:0000::2:7335 is a valid IPv6 IP address

::1 is a valid IPv6 IP address

:: is a valid IPv6 IP address

::1234:5678 is a valid IPv6 IP address

2001:db8:: is a valid IPv6 IP address

1200:0000:AB00:1234:O000:2552:7777:1313 is not a valid IPv6 IP address

0.0.0.5 is not a valid IPv6 IP address

5 is not a valid IPv6 IP address

Foo is not a valid IPv6 IP address

1::5 is a valid IPv6 IP address

Example 2: Using Regex

In this example, we used the regular expressions to validate whether the IPv6 IP address is valid or not.

Here is the regular expression which is used in this example and this is used only to validate IPv6 IP addresses.

Here is the source code of the program to validate the IPv6 IP address is valid or not.

Example 2: Using Regex
using System;
using System.Text.RegularExpressions;

namespace Tutorialsrack
{
    class Program
    {
        /* How to Validate the IPv6 IP Address is Valid or Not Using IPAddress.TryParse() in C# */
        static void Main(string[] args)
        {
            IsValid_IPv6_IPAddress("2001:0db8:85a3:0000:0000:8a2e:0370:7334");
            IsValid_IPv6_IPAddress("2001:0db8:85a3:0000:0000:8a2e:0370");
            IsValid_IPv6_IPAddress("2001:0db8:85a3:0000:0000:8a2e::5");
            IsValid_IPv6_IPAddress("2001:0db8:85a3:0000:0000::2:7335");
            IsValid_IPv6_IPAddress("::1");
            IsValid_IPv6_IPAddress("::");
            IsValid_IPv6_IPAddress("::1234:5678");
            IsValid_IPv6_IPAddress("2001:db8::");
            IsValid_IPv6_IPAddress("1200:0000:AB00:1234:O000:2552:7777:1313");
            IsValid_IPv6_IPAddress("0.0.0.5");
            IsValid_IPv6_IPAddress("5");
            IsValid_IPv6_IPAddress("Foo");
            IsValid_IPv6_IPAddress("1::5");

            //Hit ENTER to exit the program
            Console.ReadKey();
        }

        public static void IsValid_IPv6_IPAddress(string IpAddress)
        {
            Regex reg = new Regex(@"(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))");
            try
            {
                bool flag = false;
                if (!string.IsNullOrWhiteSpace(IpAddress))
                {
                    flag = reg.IsMatch(IpAddress);
                }
                if (flag)
                {
                    Console.WriteLine("{0} is a valid IPv6 IP address", IpAddress);
                }
                else
                {
                    Console.WriteLine("{0} is not a valid IPv6 IP address", IpAddress);
                }
            }
            catch (Exception) { }
        }
    }
}
Output

2001:0db8:85a3:0000:0000:8a2e:0370:7334 is a valid IPv6 IP address

2001:0db8:85a3:0000:0000:8a2e:0370 is not a valid IPv6 IP address

2001:0db8:85a3:0000:0000:8a2e::5 is a valid IPv6 IP address

2001:0db8:85a3:0000:0000::2:7335 is a valid IPv6 IP address

::1 is a valid IPv6 IP address

:: is a valid IPv6 IP address

::1234:5678 is a valid IPv6 IP address

2001:db8:: is a valid IPv6 IP address

1200:0000:AB00:1234:O000:2552:7777:1313 is not a valid IPv6 IP address

0.0.0.5 is not a valid IPv6 IP address

5 is not a valid IPv6 IP address

Foo is not a valid IPv6 IP address

1::5 is a valid IPv6 IP address

I hope this article will help you to understand how to validate whether an IPv6 IP address is valid or not in C#

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!


Related Posts



Comments

Recent Posts
Tags