Home
Manage Your Code
Snippet: Validate IP Address (C#)
Title: Validate IP Address Language: C#
Description: It has 2 parts. 1 a function that returns true if it is an IP and 2 the Regular Expression it uses to validate it. Views: 3663
Author: paul b Date Added: 4/27/2006
Copy Code  
1     ///<summary>
2        /// Validate that String is an IP address.
3        /// Between 0.0.0.0 and 255.255.255.255
4        ///</summary>
5        static bool validateIP(string strIPAddressField)
6        {
7            if (regIP.IsMatch(strIPAddressField))
8            {
9                //MessageBox.Show("Valid IP Address entered.", "Valid IP Address", MessageBoxButtons.OK, MessageBoxIcon.Information);
10                return true;
11            }
12            //MessageBox.Show(strIPAddressField);
13            return false;
14        }
15
16        /// <summary>
17        ///  Regular expression built for C# on: Wed, Apr 12, 2006, 04:13:13 PM
18        ///  Using Expresso Version: 2.1.2150, http://www.ultrapico.com
19        ///  
20        ///  A description of the regular expression:
21        ///  
22        ///  [First]: A named capture group. [2[0-4]\d|25[0-5]|[01]?\d\d?]
23        ///      Select from 3 alternatives
24        ///          2[0-4]\d
25        ///              2
26        ///              Any character in this class: [0-4]
27        ///              Any digit
28        ///          25[0-5]
29        ///              25
30        ///              Any character in this class: [0-5]
31        ///          [01]?\d\d?
32        ///              Any character in this class: [01], zero or one repetitions
33        ///              Any digit
34        ///              Any digit, zero or one repetitions
35        ///  .
36        ///  [Second]: A named capture group. [2[0-4]\d|25[0-5]|[01]?\d\d?]
37        ///      Select from 3 alternatives
38        ///          2[0-4]\d
39        ///              2
40        ///              Any character in this class: [0-4]
41        ///              Any digit
42        ///          25[0-5]
43        ///              25
44        ///              Any character in this class: [0-5]
45        ///          [01]?\d\d?
46        ///              Any character in this class: [01], zero or one repetitions
47        ///              Any digit
48        ///              Any digit, zero or one repetitions
49        ///  .
50        ///  [Third]: A named capture group. [2[0-4]\d|25[0-5]|[01]?\d\d?]
51        ///      Select from 3 alternatives
52        ///          2[0-4]\d
53        ///              2
54        ///              Any character in this class: [0-4]
55        ///              Any digit
56        ///          25[0-5]
57        ///              25
58        ///              Any character in this class: [0-5]
59        ///          [01]?\d\d?
60        ///              Any character in this class: [01], zero or one repetitions
61        ///              Any digit
62        ///              Any digit, zero or one repetitions
63        ///  .
64        ///  [Fourth]: A named capture group. [2[0-4]\d|25[0-5]|[01]?\d\d?]
65        ///      Select from 3 alternatives
66        ///          2[0-4]\d
67        ///              2
68        ///              Any character in this class: [0-4]
69        ///              Any digit
70        ///          25[0-5]
71        ///              25
72        ///              Any character in this class: [0-5]
73        ///          [01]?\d\d?
74        ///              Any character in this class: [01], zero or one repetitions
75        ///              Any digit
76        ///              Any digit, zero or one repetitions
77        ///  
78        ///  
79        /// </summary>
80        private static Regex regIP = new Regex(
81            @"(?<First>2[0-4]\d|25[0-5]|[01]?\d\d?)\.(?<Second>2[0-4]\d|25"
82            + @"[0-5]|[01]?\d\d?)\.(?<Third>2[0-4]\d|25[0-5]|[01]?\d\d?)\.(?"
83            + @"<Fourth>2[0-4]\d|25[0-5]|[01]?\d\d?)",
84            RegexOptions.IgnoreCase
85            | RegexOptions.CultureInvariant
86            | RegexOptions.IgnorePatternWhitespace
87            | RegexOptions.Compiled
88            );
Usage
If (validateIP("10.199.10.10"))
	MessageBox.Show("Yeah Valid IP")
Notes
the message box cod is only there for debugging.