473,414 Members | 1,740 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

How to validate textbox for IPV4 and IPV6 IPAddresses.

Hi,

I am working on the validations of the entered ip addresses but not able to validate using existing methods.
such as IPAddress.TryParse(addrString, out address);

so pls give me some suggessions on this as i have wasted lot of time on it.

Thanks in advance.
Feb 26 '10 #1

✓ answered by tlhintoq

You could do it through Regular Expressions (RegEx)

Expand|Select|Wrap|Line Numbers
  1. /// method to validate an IP address
  2. /// using regular expressions. The pattern
  3. /// being used will validate an ip address
  4. /// with the range of 1.0.0.0 to 255.255.255.255
  5. /// </summary>
  6. /// <param name="addr">Address to validate</param>
  7. /// <returns></returns>
  8. public bool IsValidIP(string addr)
  9. {
  10.     //create our match pattern
  11.     string pattern = @"^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.
  12.     ([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$";
  13.     //create our Regular Expression object
  14.     Regex check = new Regex(pattern);
  15.     //boolean variable to hold the status
  16.     bool valid = false;
  17.     //check to make sure an ip address was provided
  18.     if (addr == "")
  19.     {
  20.         //no address provided so return false
  21.         valid = false;
  22.     }
  23.     else
  24.     {
  25.         //address provided so use the IsMatch Method
  26.         //of the Regular Expression object
  27.         valid = check.IsMatch(addr, 0);
  28.     }
  29.     //return the results
  30.     return valid;
  31. }

2 6848
tlhintoq
3,525 Expert 2GB
You could do it through Regular Expressions (RegEx)

Expand|Select|Wrap|Line Numbers
  1. /// method to validate an IP address
  2. /// using regular expressions. The pattern
  3. /// being used will validate an ip address
  4. /// with the range of 1.0.0.0 to 255.255.255.255
  5. /// </summary>
  6. /// <param name="addr">Address to validate</param>
  7. /// <returns></returns>
  8. public bool IsValidIP(string addr)
  9. {
  10.     //create our match pattern
  11.     string pattern = @"^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.
  12.     ([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$";
  13.     //create our Regular Expression object
  14.     Regex check = new Regex(pattern);
  15.     //boolean variable to hold the status
  16.     bool valid = false;
  17.     //check to make sure an ip address was provided
  18.     if (addr == "")
  19.     {
  20.         //no address provided so return false
  21.         valid = false;
  22.     }
  23.     else
  24.     {
  25.         //address provided so use the IsMatch Method
  26.         //of the Regular Expression object
  27.         valid = check.IsMatch(addr, 0);
  28.     }
  29.     //return the results
  30.     return valid;
  31. }
Feb 26 '10 #2
Plater
7,872 Expert 4TB
Depending on your need, you would want to include 0.0.0.0 as well
Feb 26 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Agent | last post by:
I would like to invite you and your associates to attend the US IPv6 (Internet Protocol version 6) Summit, December 8-11, 2003 in Crystal City, VA, near the Pentagon. This major opportunity to...
7
by: Torsten Schmidt | last post by:
Hi, I'm trying to connect to a mysql-Server using PHP's mysql-function mysql_connect. The host on which the mysql-server is running is not the same as the host apache and php are running on. The...
3
by: MJ | last post by:
For some reason the following script does not work in Netscape/Mozilla, but works fine in IE and Opera. It is supposed to check the syntax, make sure there is a valid TLD (yes, those are all of...
2
by: PaulH | last post by:
I am attempting to write a functon that can perform IPv6 compliant pings. But, Icmp6SendEcho2 causes an access violation whenever it is called: First-chance exception at 0x76d641e8 in mping2.exe:...
2
by: prabhuram.k | last post by:
Can anybody know how to validate IPV4 and IPV6 address in PHP. I am using PHP 4.2.3 for some compatiability reason.
3
by: Jeff Jarrell | last post by:
I have one textbox on a form. Nothing else. The validate event doesn't fire. Drop another textbox on the form and the validate event fires. Set tabstop = false on the 2nd text box and the...
20
numberwhun
by: numberwhun | last post by:
Hello everyone! I am still learning (TONS every day) and having an absolute blast. Unfortunately, I have an issue that is puzzling and bewildering me. Seeing as how the best way to learn is to...
2
by: Valerie Hough | last post by:
My app has so far only encountered IPv4 addresses and I use: Dns.GetHostByName( "someOtherComputer", portNumber ).AddressList. Can someone please point me to an example of how to turn this into...
1
by: =?Utf-8?B?V2lsbGlhbSBSYW5kbGV0dA==?= | last post by:
The System.Uri class is not behaving as I expect when an IPv4 address is embedded within an IPv6 address. The following test works fine, using IPv6 only: public void...
14
by: Simon | last post by:
Hi, is there a straight forward way of converting IPv4 to IPv6? I thought that it was just a matter of converting 32 bits to 128 bits, (by adding 96 leading 0s), but that does not seem right...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.