就是一个文本框,然后一个按钮,再判断...我给高分你们大虾们
解决方案 »
- 弱弱的问一个问题
- 如何用DataTable.Compute("Count(*)", "xxxxxx")实现日期字段的过滤统计?
- 安装Oracle时网络验证未执行应该怎样解决啊
- ▲▲▲水晶报表PUSH模式问题?
- ******** 我是在简体操作系统上开发的程序(程序里的字符都是繁体的,显示也良好),现在拿到繁体的操作系统里面就产生乱码了 。
- 关于数据库数据在窗口显示的问题!
- 我要做这样的一个报表,datagrid可以做吗?
- VARCHAR类型的时间字段相减的SQL语句
- 如何将Char[]转换为Byte[]?
- 有人能告诉我吗?怎么做个浏览器
- 泛型List.Sort()多属性排序
- 如何用C#编程启动磁盘配额?这问题困倒我好多天了
参考代码:
//判断IP地址是否有效
public static bool IsRightIP(string ip)
{
if (Regex.IsMatch(ip, "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}"))
{
string[] ips = ip.Split('.');
if (ips.Length == 4 || ips.Length == 6)
{
if (System.Int32.Parse(ips[0]) < 256 && System.Int32.Parse(ips[1]) < 256 & System.Int32.Parse(ips[2]) < 256 & System.Int32.Parse(ips[3]) < 256)
return true;
else
return false;
}
else
return false;
}
else
return false;
}
IPAddress ip;
if (IPAddress.TryParse(textBox1.Text, out ip))
{
MessageBox.Show("合法!");
}
else
{
MessageBox.Show("不合法!");
}
我有个特例:
127.1你试试ping这个地址, 可以正常解析成本机的。
可是不能通过你的正则表达式。
在没有名字空间的类上面按。
using System.Text.RegularExpressions;
123 会被认为是 0.0.0.123如果想严格限定格式,用正则判断吧using System.Text.RegularExpressions;Regex reg = new Regex(@"(?n)^(([1-9]?[0-9]|1[0-9]{2}|2([0-4][0-9]|5[0-5]))\.){3}([1-9]?[0-9]|1[0-9]{2}|2([0-4][0-9]|5[0-5]))$");
if (reg.IsMatch(textBox1.Text))
{
MessageBox.Show("合法!");
}
else
{
MessageBox.Show("不合法!");
}
{
if (string.IsNullOrEmpty(IP))
return false; return Regex.IsMatch(IP, @"^((\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$");
}
using System.Text;
using System.Text.RegularExpressions;
using HKH.Common;namespace HKH.Common.Network
{
/// <summary>
///
/// </summary>
/// <res> immutable class </res>
public class IPAddress : INullable
{
protected const int IPLENGTH = 4;
protected byte[] parts = new byte[4]; public IPAddress(byte p1, byte p2, byte p3, byte p4)
{
parts[0] = p1;
parts[1] = p2;
parts[2] = p3;
parts[3] = p4;
} public IPAddress(byte[] ip)
:this(ip[0],ip[1],ip[2],ip[3])
{
} public static IPAddress Null
{
get { return NullIPAddress.Instance; }
} public static bool Validate(string IP)
{
if (string.IsNullOrEmpty(IP))
return false; return Regex.IsMatch(IP, @"^((\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$");
} public static byte[] Split(string sIP)
{
if (!Validate(sIP))
throw new IPBadFormatException(); byte[] ip = new byte[IPLENGTH];
string[] sParts = sIP.Split('.'); for (int i = 0; i < IPLENGTH; i++)
ip[i] = byte.Parse(sParts[i]); return ip;
} public virtual uint TransferToUINT()
{
return MakeLong(MakeWord(parts[3], parts[2]), MakeWord(parts[1], parts[0]));
} public virtual int Compare(IPAddress IP)
{
return (int)(this.TransferToUINT() - IP.TransferToUINT());
} #region Protected Methods protected static ushort MakeWord(byte bLow, byte bHigh)
{
return ((ushort)(((byte)(bLow)) | ((ushort)((byte)(bHigh))) << 8));
} protected static uint MakeLong(ushort bLow, ushort bHigh)
{
return ((uint)(((ushort)(bLow)) | ((uint)((ushort)(bHigh))) << 16));
} #endregion #region Base Overrides public override bool Equals(object obj)
{
if (obj == null)
return false; IPAddress ip = obj as IPAddress;
if (ip == null)
return false; return Compare(ip) == 0;
} public override int GetHashCode()
{
return this.ToString().GetHashCode();
} public override string ToString()
{
StringBuilder builder = new StringBuilder();
foreach (byte part in parts)
{
builder.Append(part);
builder.Append(Constants.Point);
}
builder.Remove(builder.Length - 1, 1); return builder.ToString();
}
#endregion #region INullable Members public virtual bool IsNull
{
get { return false; }
} #endregion
} # region Null IPAddress Class internal sealed class NullIPAddress : IPAddress
{
private const string emptyIP = "0.0.0.0"; private static NullIPAddress self = new NullIPAddress(); //Should Never use it directly
private NullIPAddress()
: base(0,0,0,0)
{
} public static IPAddress Instance
{
get { return self; }
} #region Base Class Overrides public override uint TransferToUINT()
{
return 0;
} public override string ToString()
{
return emptyIP;
} public override bool IsNull
{
get { return true; }
} #endregion
} #endregion #region IPBadFormatException Class public class IPBadFormatException : ApplicationException
{
private const string ErrorMSG = "Bad Format IP Address!"; /// <summary>
/// do not allow creation of exception with no message
/// </summary>
public IPBadFormatException()
: base(ErrorMSG)
{
} /// <summary>
/// Constructor takes problem message and caught exception
/// invokes constructor on ApplicationException
/// </summary>
public IPBadFormatException(Exception ex)
: base(ErrorMSG, ex)
{
}
} #endregion}
using System.Collections.Generic;
using System.Text;
using HKH.Common;namespace HKH.Common.Network
{
public class IPRange
{
#region Private Variables private IPAddress startIP = IPAddress.Null;
private IPAddress endIP = IPAddress.Null; #endregion #region Constructor
public IPRange(IPAddress startIP, IPAddress endIP)
{
this.startIP = startIP;
this.endIP = endIP;
} #endregion #region Properties
public IPAddress StartIP
{
get { return startIP; }
} public IPAddress EndIP
{
get { return endIP; }
}
#endregion #region Methods
public bool IsInclude(IPAddress ip)
{
return !(startIP.Compare(ip) > 0 || endIP.Compare(ip) < 0);
} public bool HasIntersection(IPRange ipRange)
{
return this.IsInclude(ipRange.startIP) || this.IsInclude(ipRange.endIP);
} public IPRange GetIntersection(IPRange ipRange)
{
if (!HasIntersection(ipRange))
throw new InvalidOperationException("The IPRanges don't have Intersection."); return new IPRange(this.IsInclude(ipRange.startIP) ? ipRange.startIP : this.startIP,
this.IsInclude(ipRange.endIP) ? ipRange.endIP : this.endIP);
} public IPRange Merge(IPRange ipRange)
{
if (!HasIntersection(ipRange))
throw new InvalidOperationException("The IPRanges don't have Intersection."); return new IPRange(this.IsInclude(ipRange.startIP) ? this.startIP : ipRange.startIP,
this.IsInclude(ipRange.endIP) ? this.endIP : ipRange.endIP);
}
#endregion #region Base Overrides public override bool Equals(object obj)
{
if (obj == null)
return false; IPRange ipRange = obj as IPRange;
if (ipRange == null)
return false; return this.startIP.Equals(ipRange.startIP) && this.endIP.Equals(ipRange.endIP);
} public override int GetHashCode()
{
return this.ToString().GetHashCode();
} public override string ToString()
{
StringBuilder builder = new StringBuilder(); builder.Append(startIP.ToString());
builder.Append(Constants.WhiteSpace);
builder.Append(Constants.Minus);
builder.Append(Constants.WhiteSpace);
builder.Append(endIP.ToString()); return builder.ToString();
}
#endregion }
}
if (IPAddress.TryParse(textBox1.Text, out ip) && textBox1.Text.Split('.').Length == 4)
{
MessageBox.Show("合法!");
}
else
{
MessageBox.Show("不合法!");
}