What's Here?
- Members: 119,059
- Replies: 436,219
- Topics: 67,434
- Snippets: 2,417
- Tutorials: 641
- Total Online: 1,445
- Members: 69
- Guests: 1,376
Who's Online?
|
Welcome to Dream.In.Code |
|
|
Getting C# Help is Easy!
Join 119,059 C# Programmers for FREE! Ask your question and get quick answers from experts. There are 1,445 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!
|
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: 10,956 |
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
|
|
|
Reference Sheets
Bye Bye Ads
Free DIC T-Shirt
Related Sites
Monthly Drawing
Partners
Top Contributors
Top 10 Kudos This Month
|