In any project, many times the IP4 address needs to be validated. Here I have shared with you below the code of a c # method as IP4 address validator, which can be used ready-made in any c # project.
The method is quite useful. Pass in any IP address as parameter while calling this method will return you true on success, and false on failure.
/// Check IP Address, will accept 0.0.0.0 as a valid IP
/// <param name="strIP">0.0.0.0 style IP address</param>
public static Boolean CheckIPValid(String strIP)
// Split string by ".", check that array length is 3
char chrFullStop = '.';
string arrOctets = strIP.Split(chrFullStop);
if (arrOctets.Length != 4)
// Check each substring checking that the int value
// is less than 255
// and that is char length is !> 2
Int16 MAXVALUE = 255;
Int32 temp; // Parse returns Int32
foreach (String strOctet in arrOctets)
if (strOctet.Length > 3)
temp = int.Parse(strOctet);
if (temp > MAXVALUE)
Here in the example, this method is declared a static. You may declare it as your project requirement, i.e., simply as private or public scope.