Home > C# Snippets, Development > Validate IPv4 Address

Validate IPv4 Address

May 9th, 2011

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;
}
  1. June 25th, 2013 at 18:05 | #1

    Do you mind if I quote a couple of your posts as long as I provide credit and sources back to your blog?
    My blog site is in the exact same area of interest as yours and my users would really benefit from a lot of the information you present here.
    Please let me know if this alright with you. Appreciate it!

Comments are closed.
%d bloggers like this: