Posted By

jx_boi on 08/30/10


Tagged

check ip


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

franksmile0310
Tyster
ovuncgursoy


IsValidIP


 / Published in: C#
 

  1. public bool IsValidIP(string addr)
  2. {
  3. //create our match pattern
  4. string pattern = @"\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b";
  5. //create our Regular Expression object
  6. Regex check = new Regex(pattern);
  7. //boolean variable to hold the status
  8. bool valid = false;
  9. //check to make sure an ip address was provided
  10. if (addr == "")
  11. {
  12. //no address provided so return false
  13. valid = false;
  14. }
  15. else
  16. {
  17. //address provided so use the IsMatch Method
  18. //of the Regular Expression object
  19. valid = check.IsMatch(addr, 0);
  20. }
  21. //return the results
  22. return valid;
  23. }

Report this snippet  

You need to login to post a comment.