3 Replies - 2398 Views - Last Post: 18 October 2012 - 06:50 AM

#1 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1658
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Validate IP addres with TryParse

Posted 31 October 2007 - 08:06 AM

Description: Pass the method an address. Make sure to add a reference to the System.Net NamespaceThis is a snippet I use (in C# 2.0) using the TryParse Method of the IPAddress Class
//
//method to validate an IP address using
//the TryParse Method of the IPAddress class
//
//address to validate
//
public bool IsValidIP(string addr)
{
    //create an IPAddress variable, TryParse
    //requires an "out" value that is of
    //the type IPAddress
    IPAddress ip;
    //boolean variable to hold the status
    bool valid = false;
    //check to make sure an ip address was provided
    if (string.IsNullOrEmpty(addr))
    {
        //address wasnt provided so return false
        valid = false;
    }
    else
    {
        //use TryParse to see if this is a 
        //valid ip address. TryParse returns a
        //boolean based on the validity of the
        //provided address, so assign that value
        //to our boolean variable
        valid = IPAddress.TryParse(addr, ip);
    }
    //return the value
    return valid;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Validate IP addres with TryParse

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1658
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Validate IP addres with TryParse

Posted 31 October 2007 - 08:06 AM

Description: Pass the method an address. Make sure to add a reference to the System.Net NamespaceThis is a snippet I use (in C# 2.0) using the TryParse Method of the IPAddress Class
//
//method to validate an IP address using
//the TryParse Method of the IPAddress class
//
//address to validate
//
public bool IsValidIP(string addr)
{
    //create an IPAddress variable, TryParse
    //requires an "out" value that is of
    //the type IPAddress
    IPAddress ip;
    //boolean variable to hold the status
    bool valid = false;
    //check to make sure an ip address was provided
    if (string.IsNullOrEmpty(addr))
    {
        //address wasnt provided so return false
        valid = false;
    }
    else
    {
        //use TryParse to see if this is a 
        //valid ip address. TryParse returns a
        //boolean based on the validity of the
        //provided address, so assign that value
        //to our boolean variable
        valid = IPAddress.TryParse(addr, out ip);
    }
    //return the value
    return valid;
}

Was This Post Helpful? 0
  • +
  • -

#3 Machtyn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 27-February 09

Re: Validate IP addres with TryParse

Posted 27 February 2009 - 09:11 AM

The TryParse is slightly incorrect: valid = IPAddress.TryParse( addr, out ip );
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1658
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Validate IP addres with TryParse

Posted 27 February 2009 - 09:12 AM

@ Machtyn : Thanks for pointing that out. It has been fixed :)
Was This Post Helpful? 0
  • +
  • -

#5 pmmout  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 13-October 11

Re: Validate IP addres with TryParse

Posted 18 October 2012 - 06:50 AM

Other way public bool IsValidIp(string addr) { IPAddress ip; bool valid = !string.IsNullOrEmpty(addr) && IPAddress.TryParse(addr, out ip); return valid; }
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1