In this article, you will learn how to validate an IPv4 IP address is Valid or not in C#. There are various ways to validate the IP address is valid or Not such as using IPAddress.TryParse()
method, using regex, or you can create your own method to validate the IP Address.
Here is some Example of programs to validate the IPv4 IPAddress is valid or Not in C#.
In this example, we will use the IPAddress.TryParse()
to validate the IPv4 IP Address. But this method has some limitations that it verifies if a string could be converted to an IP address, thus if it is supplied with a string value like "6", it considers it as "0.0.0.6". So we can overcome this problem of IPAddress.TryParse()
by counting 3 dots present in the given string or not before passing the string to this IPAddress.TryParse()
method.
Here is the source code of the Program to validate the IPv4 IP Address is valid or Not
using System;
using System.Linq;
using System.Net;
namespace Tutorialsrack
{
class Program
{
/* How to Validate the IPv4 IP Address is Valid or Not in C# */
static void Main(string[] args)
{
IsValidIPAddress("201.52.56.33");
IsValidIPAddress("172.02.02.30");
IsValidIPAddress("127.0.0.1");
IsValidIPAddress("127.0.526.1");
IsValidIPAddress("5");
IsValidIPAddress("0.0.0.5");
IsValidIPAddress("0.0.0.0");
IsValidIPAddress("0.0.5");
//Hit ENTER to exit the program
Console.ReadKey();
}
public static void IsValidIPAddress(string IpAddress)
{
try
{
IPAddress IP;
if (IpAddress.Count(c => c == '.') == 3)
{
bool flag = IPAddress.TryParse(IpAddress, out IP);
if (flag)
{
Console.WriteLine("{0} is a valid IP address", IpAddress);
}
else
{
Console.WriteLine("{0} is not a valid IP address", IpAddress);
}
}
else
{
Console.WriteLine("{0} is not a valid IP address", IpAddress);
}
}
catch (Exception) { }
}
}
}
201.52.56.33 is a valid IP address
172.02.02.30 is not a valid IP address
127.0.0.1 is a valid IP address
127.0.526.1 is not a valid IP address
5 is not a valid IP address
0.0.0.5 is a valid IP address
0.0.0.0 is a valid IP address
0.0.5 is not a valid IP address
In this example, we used the regex to validate whether an IPv4 IP address is a valid IP address or not.
Here is the source code of the program to Validate whether the IPv4 IP address is valid or not.
using System;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
namespace Tutorialsrack
{
class Program
{
/* How to Validate the IPv4 IP Address is Valid or Not Using Regex in C# */
static void Main(string[] args)
{
IsValidIPAddress("201.52.56.33");
IsValidIPAddress("172.02.02.30");
IsValidIPAddress("127.0.0.1");
IsValidIPAddress("127.0.526.1");
IsValidIPAddress("5");
IsValidIPAddress("0.0.0.5");
IsValidIPAddress("0.0.0.0");
IsValidIPAddress("0.0.5");
//Hit ENTER to exit the program
Console.ReadKey();
}
public static void IsValidIPAddress(string IpAddress)
{
Regex validIpV4AddressRegex = new Regex(@"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$", RegexOptions.IgnoreCase);
try
{
if (!string.IsNullOrWhiteSpace(IpAddress))
{
bool flag = validIpV4AddressRegex.IsMatch(IpAddress.Trim());
if (flag)
{
Console.WriteLine("{0} is a valid IP address", IpAddress);
}
else
{
Console.WriteLine("{0} is not a valid IP address", IpAddress);
}
}
}
catch (Exception) { }
}
}
}
201.52.56.33 is a valid IP address
172.02.02.30 is not a valid IP address
127.0.0.1 is a valid IP address
127.0.526.1 is not a valid IP address
5 is not a valid IP address
0.0.0.5 is a valid IP address
0.0.0.0 is a valid IP address
0.0.5 is not a valid IP address
In this example, we used the string.Split()
method to split the string from the dot(.) and check the length of the split values array which is equal to 4 or not if true then using the LINQ All()
method and for parsing byte.TryParse()
method. This is a much better approach than the IPAddress.TryParse()
method.
Here is the source code of the program to validate the IPv4 IP address is valid or not.
using System;
using System.Linq;
namespace Tutorialsrack
{
class Program
{
/* How to Validate the IPv4 IP Address is Valid or Not in C# */
static void Main(string[] args)
{
IsValidIPAddress("201.52.56.33");
IsValidIPAddress("172.02.02.30");
IsValidIPAddress("127.0.0.1");
IsValidIPAddress("127.0.526.1");
IsValidIPAddress("5");
IsValidIPAddress("0.0.0.5");
IsValidIPAddress("0.0.0.0");
IsValidIPAddress("0.0.5");
//Hit ENTER to exit the program
Console.ReadKey();
}
public static void IsValidIPAddress(string IpAddress)
{
try
{
if (!String.IsNullOrWhiteSpace(IpAddress))
{
string[] splitValues = IpAddress.Split('.');
if (splitValues.Length == 4)
{
byte tempForParsing;
bool flag = splitValues.All(r => byte.TryParse(r, out tempForParsing));
if (flag)
{
Console.WriteLine("{0} is a valid IP address", IpAddress);
}
else
{
Console.WriteLine("{0} is not a valid IP address", IpAddress);
}
}else
{
Console.WriteLine("{0} is a not a valid IP address", IpAddress);
}
}
}
catch (Exception){}
}
}
}
201.52.56.33 is a valid IP address
172.02.02.30 is not a valid IP address
127.0.0.1 is a valid IP address
127.0.526.1 is not a valid IP address
5 is not a valid IP address
0.0.0.5 is a valid IP address
0.0.0.0 is a valid IP address
0.0.5 is not a valid IP address
In this example, we used the string.Split()
function to split the string into the array and check the length of the array which is equal to 4 or not, then after we check each value which is less than or equal to 255. All checks found correct then it is a valid IPv4 IP address. Otherwise, it is not a valid IPv4 IP Address.
Here is the source code of the program to validate an IPv4 IP Address or not.
using System;
namespace Tutorialsrack
{
class Program
{
/* How to Validate the IPv4 IP Address is Valid or Not in C# */
static void Main(string[] args)
{
IsValidIPAddress("201.52.56.33");
IsValidIPAddress("172.02.02.30");
IsValidIPAddress("127.0.0.1");
IsValidIPAddress("127.0.526.1");
IsValidIPAddress("5");
IsValidIPAddress("0.0.0.5");
IsValidIPAddress("0.0.0.0");
IsValidIPAddress("0.0.5");
//Hit ENTER to exit the program
Console.ReadKey();
}
public static void IsValidIPAddress(string IpAddress)
{
try
{
bool flag = false;
if (IpAddress != null || IpAddress.Length != 0)
{
String[] parts = IpAddress.Split(new[] { "." }, StringSplitOptions.None);
if (parts.Length == 4)
{
if (IpAddress.EndsWith("."))
{
flag = false;
}
else
{
foreach (String s in parts)
{
int i = Int32.Parse(s);
if ((i < 0) || (i > 255))
{
flag = false;
break;
}
else
{
flag = true;
}
}
}
}
}
if (flag)
{
Console.WriteLine("{0} is a valid IP address", IpAddress);
}
else
{
Console.WriteLine("{0} is not a valid IP address", IpAddress);
}
}
catch (Exception) { }
}
}
}
201.52.56.33 is a valid IP address
172.02.02.30 is not a valid IP address
127.0.0.1 is a valid IP address
127.0.526.1 is not a valid IP address
5 is not a valid IP address
0.0.0.5 is a valid IP address
0.0.0.0 is a valid IP address
0.0.5 is not a valid IP address
You can use any one of them to validate whether a given IPv4 IP address is valid or not.
I hope this article will help you to understand how to validate whether an IPv4 IP address is valid or not in C#
Share your valuable feedback, please post your comment at the bottom of this article. Thank you!
Comments