How to validate IP address in C#?
In this short tutorial, we will implement functionality to validate IP address passed as a string. We are going to focus only on the IPv4.
First of all, IP address consists of 4 parts, called octets, which are 8-bits numbers, each ranging from 0 to 255. Parts are separated by dots.
Firstly, I want to show how we can implement a simple algorithm, which checks conditions one by one.
There are some examples of correct addresses:
"1.2.3.4"
"123.45.67.89"
"137.255.156.100"
"0.0.0.0"
Those which are incorrect may look like this:
""
"abc.def.ghi.jkl"
"123.456.789.0"
"12.34.56"
"12.34.56.00"
"12.34.56 .1"
Assuming that our function receives an argument: string ipAddress we can check if this string contains four parts mentioned above:
var ipParts = ipAddress.Split(".");
if (ipParts.Length != 4)
{
return false;
}
This code will check as well if our string is not empty. Code below will check if there are no leading zeroes in each part of the address. However, keep in mind that a single zero is permitted.
if (part.StartsWith("0") && part.Length > 1)
{
return false;
}
Next, we should check if there is no whitespace. Such address: "12.34.56 .1" is incorrect.
if (part.Length != part.Trim().Length)
{
return false;
}
The last thing we want to is to test if part of an address is an integer. It must be greater or equal to 0 and smaller or equal to 255. The code below will test both conditions:
int number;
var result = Int32.TryParse(part, out number);
if (!result || number > 255 || number < 0)
{
return false;
}
Our whole function looks like this:
public bool isValidIp(string ipAddress)
{
var ipParts = ipAddress.Split(".");
if (ipParts.Length != 4)
{
return false;
}
foreach (var part in ipParts)
{
if (part.StartsWith("0") && part.Length > 1)
{
return false;
}
if (part.Length != part.Trim().Length)
{
return false;
}
int number;
var result = Int32.TryParse(part, out number);
if (!result || number > 255 || number < 0)
{
return false;
}
}
return true;
}
This solution is sufficient to check provided IP address. Is it the only way to do it? Is it good practice? There is another approach that uses the built-in class.
IPAddress class from System.Net
namespace enables us to use a special method to parse provided string and save it as IPAddress instance. We can use TryParse
function. This function checks if a provided string is the correct IP address and then returns a boolean value and created class instance. You can find the simplest usage below:
IPAddress ip;
var result = IPAddress.TryParse(ipAddress, out ip);
return result;
Unfortunately, it will not work that way. The parser will accept every string that can be parsed as Int64. It means that result will be true even if ipAddress will have the value of "1". Address "12.34.56" will be returned as a "12.34.0.56" and result will be true. To bypass this behavior we need to check if a parsed address is the same as a provided string: ip.ToString() == ipAddress. Below is the entire function:
public bool isValidIp(string ipAddress)
{
IPAddress ip;
var result = IPAddress.TryParse(ipAddress, out ip);
return result && ip.ToString() == ipAddress;
}
As for me, the latest solution is the cleanest and most effective to use. I find it as a good practice.