What's Here?
- Members: 149,027
- Replies: 505,370
- Topics: 79,609
- Snippets: 2,663
- Tutorials: 705
- Total Online: 1,884
- Members: 80
- Guests: 1,804
|
This is a snippet I use to validate IP address using regular expressions. I use this when working in .Net 1.1 as the TryParse method isnt available until 2.0
|
Submitted By: PsychoCoder
|
|
Rating:

|
|
Views: 14,680 |
Language: C#
|
|
Last Modified: October 31, 2007 |
Instructions: Pass the method an ip address. Add
using System.Net
To your code file |
Snippet
/// <summary>
/// method to validate an IP address
/// using regular expressions. The pattern
/// being used will validate an ip address
/// with the range of 1.0.0.0 to 255.255.255.255
/// </summary>
/// <param name="addr">Address to validate</param>
/// <returns></returns>
public bool IsValidIP(string addr)
{
//create our match pattern
string pattern = @"^([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}$";
//create our Regular Expression object
Regex check = new Regex (pattern );
//boolean variable to hold the status
bool valid = false;
//check to make sure an ip address was provided
if (addr == "")
{
//no address provided so return false
valid = false;
}
else
{
//address provided so use the IsMatch Method
//of the Regular Expression object
valid = check.IsMatch(addr, 0);
}
//return the results
return valid;
}
Copy & Paste
|
|
|
Be Social
Reference Sheets
Bye Bye Ads
Monthly Drawing
Top Contributors
Top 10 Kudos This Month
|