validate an IP

  • Thread starter Thread starter Siva
  • Start date Start date
Hi Siva!

Siva said:
i need a c# sample to validate an IP.

What do you have to validate? Maybe the IPAdress.TryParse Function is
already all you need?

With kind regards,

Konrad
 
given an ip address the method has to verify that it falls within the range
1.0.0.0 to 255.255.255.255
 
Hello Konrad,
provided and ip, i want it to be in this range between 1.0.0.0 to
255.255.255.255, something to do with regex.
Siva
 
Siva said:
given an ip address the method has to verify that it falls within the range
1.0.0.0 to 255.255.255.255

As a tip to using Google to find information like that more quickly:

http://lmgtfy.com/?q=regex+validate+ip+c#

The first hit I get, http://www.dreamincode.net/code/snippet1378.htm,
shows what appears to be a correct example for an IPv4 address, using
the regular expression

^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$
 
given an ip address the method has to verify that it falls within the range
1.0.0.0 to 255.255.255.255

And IPAddress.TryParse does not provide that ??

Arne
 
Hi Siva!

Siva said:
Hello Konrad,
provided and ip, i want it to be in this range between 1.0.0.0 to
255.255.255.255, something to do with regex.

Why do you want to do it with regexp? Of course it could be done, but it
is not such an easy regexp so I would not do it that way.

I already pointed you to the IPAddress.TryParse() Function. This has the
big advantage, that even IPv6 is supported.
(Just reading the code makes a big difference. A TryParse call is easy
to understand but try to really understand the reg-exp!)

So I would say that this is a much better solution than a regexp.

With kind regards,
 
Back
Top