Archive

Posts Tagged ‘Address’

Validate IPv4 Address

May 9th, 2011 1 comment

I’ve been looking for a very simple way to validate IPv4 addresses entered into a textbox. There are quite a few options I found, but none that really suited the simple functionality I was after.

This is the code snippet of what I have implemented:

public static bool CheckIpAddress(string addressIp)
{
    try
    {
        string[] splitIp = addressIp.Split('.');

        if (splitIp.Length != 4)
        {
            return false;
        }

        if (splitIp.Select(Int32.Parse).Any(result => result < 0 || 
                                                      result > 255))
        {
            return false;
        }
    }
    catch (Exception)
    {
        return false;
    }
    return true;
}
%d bloggers like this: