Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8195809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:15:47+00:00 2026-06-07T05:15:47+00:00

I have a method to validate a parameter IP Address. Being new to development

  • 0

I have a method to validate a parameter IP Address. Being new to development as a whole I would like to know if there is a better way of doing this.

/// <summary>
/// Check IP Address, will accept 0.0.0.0 as a valid IP
/// </summary>
/// <param name="strIP"></param>
/// <returns></returns>
public bool CheckIPValid(string strIP)
{
    //  Split string by ".", check that array length is 3
    char chrFullStop = '.';
    string[] arrOctets = strIP.Split(chrFullStop);
    if (arrOctets.Length != 4)
    {
        return false;
    }
    //  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)
        {
            return false;
        }

        temp = int.Parse(strOctet);
        if (temp > MAXVALUE)
        {
            return false;
        }
    }
    return true;
}

Its simple (I could do it) but it seems to do the trick.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T05:15:48+00:00Added an answer on June 7, 2026 at 5:15 am

    The limitation with IPAddress.TryParse method is that it verifies if a string could be converted to IP address, thus if it is supplied with a string value like "5", it consider it as "0.0.0.5".

    Another approach to validate an IPv4 could be following :

    public bool ValidateIPv4(string ipString)
    {
        if (String.IsNullOrWhiteSpace(ipString))
        {
            return false;
        }
    
        string[] splitValues = ipString.Split('.');
        if (splitValues.Length != 4)
        {
            return false;
        }
    
        byte tempForParsing;
    
        return splitValues.All(r => byte.TryParse(r, out tempForParsing));
    }
    

    It could be tested like:

    List<string> ipAddresses = new List<string>
    {
        "2",
        "1.2.3",
        "1.2.3.4",
        "255.256.267.300",
        "127.0.0.1",
    };
    foreach (var ip in ipAddresses)
    {
        Console.WriteLine($"{ip} ==> {ValidateIPv4(ip)}");
    }
    

    The output will be:

    2 ==> False
    1.2.3 ==> False
    1.2.3.4 ==> True
    255.256.267.300 ==> False
    127.0.0.1 ==> True
    

    You can also use IPAddress.TryParse but it has the limitations and could result in incorrect parsing.

    System.Net.IPAddress.TryParse Method

    Note that TryParse returns true if it parsed the input successfully,
    but that this does not necessarily mean that the resulting IP address
    is a valid one. Do not use this method to validate IP addresses.

    But this would work with normal string containing at least three dots. Something like:

    string addrString = "192.168.0.1";
    IPAddress address;
    if (IPAddress.TryParse(addrString, out address)) {
           //Valid IP, with address containing the IP
    } else {
           //Invalid IP
    }
    

    With IPAddress.TryParse you can check for existence of three dots and then call TryParse like:

    public static bool ValidateIPv4(string ipString)
    {
        if (ipString.Count(c => c == '.') != 3) return false;
        IPAddress address;
        return IPAddress.TryParse(ipString, out address);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method in controller that receives a parameter like this: def example
There is a generic method that takes a class as parameter and I have
Is there a way to pass dynamic parameters into a custom jquery validate method?
i have override validate method and added errors using addFieldError(test, test print); and in
Question Is it posible to have the Validation.Validate() method of the Validation Application Block
I have method that returned NSManagedObject and I don't know what kind of NSManagedObject
I have opended a question about repeated parameter-checks in public methods. The result there
I have a method GetColors which takes a GetColorIdsRQ as a parameter and returns
I have several contact forms, which all use the same method to to validate
Every method accepts a set of parameter values. Should we always validate the non-nullness

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.