Validate IP

  • Thread starter Thread starter Dave
  • Start date Start date
D

Dave

Hello.

Is there any method to validate if string contains valid IP address?
I also need the method to valideta email address in string.

Thank you.
 
Oh! it's too dificalt for me can you show me it for IP validating please?

Sam Martin said:
you need to use regular expressions.

i.e. to validate a uk post code example

string postCodeRegEx =
@"(^((([A-PR-UWYZ])([0-9][0-9A-HJKS-UW]?))|(([A-PR-UWYZ][A-HK-Y])([0-9][0-9ABEHMNPRV-Y]?))\s{0,2}(([0-9])([ABD-HJLNP-UW-Z])([ABD-HJLNP-UW-Z])))|(((GI)(R))\s{0,2}((0)(A)(A)))$)|(^
*$)";

if (System.Text.RegularExpressions.Regex.IsMatch("W1A 1BC",postCodeRegEx)
== false) {
MessageBox.Show("Not a valid post code");
}

Regular expressions look fairly daunting, but like a complex SQL query, if
you do it in parts, they're fairly straight forward.

You need to know about www.regexlib.com also, otherwise you'll spend ages
reinventing the wheel. Here you can find literally 1,000s of reg
expressions people have already written.
There are also tools for testing them, etc.

HTH
Sam


Dave said:
Hello.

Is there any method to validate if string contains valid IP address?
I also need the method to valideta email address in string.

Thank you.
 
you need to use regular expressions.

i.e. to validate a uk post code example

string postCodeRegEx =
@"(^((([A-PR-UWYZ])([0-9][0-9A-HJKS-UW]?))|(([A-PR-UWYZ][A-HK-Y])([0-9][0-9ABEHMNPRV-Y]?))\s{0,2}(([0-9])([ABD-HJLNP-UW-Z])([ABD-HJLNP-UW-Z])))|(((GI)(R))\s{0,2}((0)(A)(A)))$)|(^
*$)";

if (System.Text.RegularExpressions.Regex.IsMatch("W1A 1BC",postCodeRegEx) ==
false) {
MessageBox.Show("Not a valid post code");
}

Regular expressions look fairly daunting, but like a complex SQL query, if
you do it in parts, they're fairly straight forward.

You need to know about www.regexlib.com also, otherwise you'll spend ages
reinventing the wheel. Here you can find literally 1,000s of reg
expressions people have already written.
There are also tools for testing them, etc.

HTH
Sam
 
you're having a laugh right, all you had to do was get another regular
expression from the site listed in both responses to your post

one Reg ex i got from that very site after search for "IP" was
@"\b(([01]?\d?\d|2[0-4]\d|25[0-5])\.){3}([01]?\d?\d|2[0-4]\d|25[0-5])\b"(try
it, http://www.regexlib.com/Search.aspx >> "IP" >> Search)

If you send me you prject specification, i'll code it all for you

HTH
Sam

(ps, im only joking)


Dave said:
Oh! it's too dificalt for me can you show me it for IP validating please?

Sam Martin said:
you need to use regular expressions.

i.e. to validate a uk post code example

string postCodeRegEx =
@"(^((([A-PR-UWYZ])([0-9][0-9A-HJKS-UW]?))|(([A-PR-UWYZ][A-HK-Y])([0-9][0-9ABEHMNPRV-Y]?))\s{0,2}(([0-9])([ABD-HJLNP-UW-Z])([ABD-HJLNP-UW-Z])))|(((GI)(R))\s{0,2}((0)(A)(A)))$)|(^
*$)";

if (System.Text.RegularExpressions.Regex.IsMatch("W1A 1BC",postCodeRegEx)
== false) {
MessageBox.Show("Not a valid post code");
}

Regular expressions look fairly daunting, but like a complex SQL query,
if you do it in parts, they're fairly straight forward.

You need to know about www.regexlib.com also, otherwise you'll spend ages
reinventing the wheel. Here you can find literally 1,000s of reg
expressions people have already written.
There are also tools for testing them, etc.

HTH
Sam


Dave said:
Hello.

Is there any method to validate if string contains valid IP address?
I also need the method to valideta email address in string.

Thank you.
 
Dave,

To validate an IP address, use the static Parse method on the IPAddress
class in the System.Net namespace.

To validate the email address in a string, I would use the Uri class,
adding "mailto:" before the address, and trying to create a Uri instance
from that. If it throws an exception, then you know it is not valid.

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Regex with time 3
validate an IP 6
Null and Generic Type 2
Help with generics 2
Null, Empty 5
Uri 3
Subclassing entity object at runtime 4
Email Bounce Error 3

Back
Top