有关验证url 地址 和 ip 地址
2011-04-15 15:24
239 查看
第一种:
验证ip地址用正则:
if (System.Text.Encoding.Default.GetByteCount(CheckIp) > 250)
{
sb.AppendLine("请输入250个字符(或125个汉字)以内的ip地址");
}
Regex reg = new Regex("^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$");
string[] checkIp = CheckIp.Split('|'); //用|分割多个ip地址判断
for (int i = 0; i < checkIp.Count(); i++)
{
if (!reg.IsMatch(checkIp[i].Trim()))
{
sb.AppendLine("ip地址格式不正确");
break;
}
}
CheckIp = CheckIp.Replace(" ", "").Trim();
验证url地址正则
if (System.Text.Encoding.Default.GetByteCount(GatewayUrl) > 200)
{
sb.AppendLine("请输入200个字符(或100个汉字)以内的接口提交地址");
}
GatewayUrl = GatewayUrl.Replace(Environment.NewLine, "").ToLower();
if (!GatewayUrl.StartsWith("http://") && !GatewayUrl.StartsWith("https://"))
{
GatewayUrl = "http://" + GatewayUrl;//(默认输入www.baidu.com这样的网址会默认加上http://开头)
}
else
{
if (GatewayUrl.StartsWith("http://http://") || GatewayUrl.StartsWith("http://https://"))
{
GatewayUrl = "http://" + GatewayUrl.Replace("http://", "").Replace("https://", "");//(输入两个会替换一个为空)
}
else if (GatewayUrl.StartsWith("https://https://") || GatewayUrl.StartsWith("https://http://"))
{
GatewayUrl = "https://" + GatewayUrl.Replace("http://", "").Replace("https://", "");
}
}
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?");
if (!reg.IsMatch(GatewayUrl))
{
sb.AppendLine("地址格式不正确");
}
第二种:
加上using system.Net命名空间
if (string.isnullorEmpty(TextBox.Text))
{
Response.Write("请输入文本框内容!");
return;
}
else
{
IpAddress ip;
if (!IpAddress.TryPase(TextBox.Text,out ip))
{
Response.write("输入的ip地址无效");
return;
}
}
uri ul
if (!uri.TryPase(TextBox.Text,urikind.(绝对的 相对的 不确定的 )out ul))
{
respon.write("url地址不正确!");
}
第一种方法是验证比较通过的,各方面都考虑到 比如相对的地址 大小写转换 断行处理
第二种方法是狼子和我说的简单的验证但ip不行输入123456也是有效地ip地址了 所以这种方法不严谨
验证ip地址用正则:
if (System.Text.Encoding.Default.GetByteCount(CheckIp) > 250)
{
sb.AppendLine("请输入250个字符(或125个汉字)以内的ip地址");
}
Regex reg = new Regex("^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$");
string[] checkIp = CheckIp.Split('|'); //用|分割多个ip地址判断
for (int i = 0; i < checkIp.Count(); i++)
{
if (!reg.IsMatch(checkIp[i].Trim()))
{
sb.AppendLine("ip地址格式不正确");
break;
}
}
CheckIp = CheckIp.Replace(" ", "").Trim();
验证url地址正则
if (System.Text.Encoding.Default.GetByteCount(GatewayUrl) > 200)
{
sb.AppendLine("请输入200个字符(或100个汉字)以内的接口提交地址");
}
GatewayUrl = GatewayUrl.Replace(Environment.NewLine, "").ToLower();
if (!GatewayUrl.StartsWith("http://") && !GatewayUrl.StartsWith("https://"))
{
GatewayUrl = "http://" + GatewayUrl;//(默认输入www.baidu.com这样的网址会默认加上http://开头)
}
else
{
if (GatewayUrl.StartsWith("http://http://") || GatewayUrl.StartsWith("http://https://"))
{
GatewayUrl = "http://" + GatewayUrl.Replace("http://", "").Replace("https://", "");//(输入两个会替换一个为空)
}
else if (GatewayUrl.StartsWith("https://https://") || GatewayUrl.StartsWith("https://http://"))
{
GatewayUrl = "https://" + GatewayUrl.Replace("http://", "").Replace("https://", "");
}
}
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?");
if (!reg.IsMatch(GatewayUrl))
{
sb.AppendLine("地址格式不正确");
}
第二种:
加上using system.Net命名空间
if (string.isnullorEmpty(TextBox.Text))
{
Response.Write("请输入文本框内容!");
return;
}
else
{
IpAddress ip;
if (!IpAddress.TryPase(TextBox.Text,out ip))
{
Response.write("输入的ip地址无效");
return;
}
}
uri ul
if (!uri.TryPase(TextBox.Text,urikind.(绝对的 相对的 不确定的 )out ul))
{
respon.write("url地址不正确!");
}
第一种方法是验证比较通过的,各方面都考虑到 比如相对的地址 大小写转换 断行处理
第二种方法是狼子和我说的简单的验证但ip不行输入123456也是有效地ip地址了 所以这种方法不严谨
相关文章推荐
- 常用表单验证类,验证EMAIL地址,电话号码,指定长度数字,汉字,邮编,URL地址等2007-10-11 09:14
- 20170408_OSI参考模型与TCP/IP模型与输入URL地址按下enter键以后发生了什么?
- Javascript验证用户输入URL地址是否正确
- PHP自带方法验证邮箱、URL、IP是否合法的函数
- 正则表达式验证url、时间、ip
- jquery中邮箱地址 URL网站地址正则验证实例
- 验证url是否为图片地址,判断
- IP 地址聚合 经典算法 已经过验证
- php内置函数实现 验证邮箱,url地址格式是否合法
- PHP自带函数验证邮箱、URL、IP是否合法
- PHP自带方法验证邮箱、URL、IP是否合法
- filter_var() 验证邮箱、ip、url的格式 php
- 获取地址栏的IP和端口,将URL地址图片转换为二进制
- 验证url地址的有效性
- Ip 子网掩码 物理地址 验证
- 几个与IP地址类型有关的术语 推荐
- 验证url 地址是否是图片
- PHP获取 当前页面名称、主机名、URL完整地址、URL参数、获取IP
- Extjs中手动验证多个url地址和Extjs验证原理
- url、IP端口、汉字、数字范围正则表达式验证