Verify that the user entered the IP address correctly

I am currently working on a C # project where I need to check the text that the user entered into the text box.

One of the necessary checks is that it checks to see if the IP address is entered correctly.

How will I do this IP address check.

Thanks for any help you can provide.

+6
source share
6 answers

It seems that you are only interested in checking the IP addresses of IP addresses in XXXX format. If so, this code is direct for this task:

string ip = "127.0.0.1"; string[] parts = ip.Split('.'); if (parts.Length < 4) { // not a IPv4 string in XXXX format } else { foreach(string part in parts) { byte checkPart = 0; if (!byte.TryParse(part, out checkPart)) { // not a valid IPv4 string in XXXX format } } // it is a valid IPv4 string in XXXX format } 
+5
source

You can use IPAddress.Parse Method.NET Framework 1.1 . Or, if you are using .NET 4.0, see the Documentation for IPAddress.TryParse Method.NET Framework 4 .

This method determines whether the contents of the string match a valid IP address. In .NET 1.1, the return value is the IP address. In .NET 4.0, a return value indicates success / failure, and the IP address is returned to the IP address passed as the out parameter in the method call.

edit: ok I’ll play the game for the bounty :) Here is an example implementation as an extension method that requires C # 3+ and .NET 4.0:

 using System; using System.Net; using System.Text.RegularExpressions; namespace IPValidator { class Program { static void Main (string[] args) { Action<string> TestIP = (ip) => Console.Out.WriteLine (ip + " is valid? " + ip.IsValidIP ()); TestIP ("99"); TestIP ("99.99.99.99"); TestIP ("255.255.255.256"); TestIP ("abc"); TestIP ("192.168.1.1"); } } internal static class IpExtensions { public static bool IsValidIP (this string address) { if (!Regex.IsMatch (address, @"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b")) return false; IPAddress dummy; return IPAddress.TryParse (address, out dummy); } } } 
+6
source

If you want to see if an IP address really exists, you can use the Ping class.

+1
source

insta answer was closer before he added the wrong regular expression.

 public static bool IsValidIP(string ipAddress) { IPAddress unused; return IPAddress.TryParse(ipAddress, out unused); } 

Or, since the OP does not want to include integer IPv4 addresses that are not full dotted squares:

 public static bool IsValidIP(string ipAddress) { IPAddress unused; return IPAddress.TryParse(ipAddress, out unused) && ( unused.AddressFamily != AddressFamily.InterNetwork || ipAddress.Count(c => c == '.') == 3 ); } 

Testing:

IsValidIP("fe80::202:b3ff:fe1e:8329") returns true (correct).

IsValidIP("127.0.0.1") returns true (correct).

IsValidIP("What an IP address?") Returns false (correct).

IsValidIP("127") returns true with the first version false with the second (correct).

IsValidIP("127.0") returns true with the first version false with the second (correct).

+1
source

I would use a regex.

 ^((25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)\.){3}(25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)$ 

The use of these groups may be clearer. It is written in Ruby. I do not know C #, but I assume that regular expression support is complete in this language and that the named groups may exist.

  /(?<number>(25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){0}^(\g<number>\.){3}\g<number>$/ 
+1
source

Here is my solution:

  using System.Net.NetworkInformation;
 using System.Net;
  /// <summary> /// Return true if the IP address is valid. /// </summary> /// <param name="address"></param> /// <returns></returns> public bool TestIpAddress (string address) { PingReply reply; Ping pingSender = new Ping (); try { reply = pingSender.Send (address); } catch (Exception) { return false; } return reply.Status == IPStatus.Success; } 
0
source

Source: https://habr.com/ru/post/899311/


All Articles