• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
小香猪 工作室~~
————含泪播种的人一定能含笑收获 !!!

asp.net、 C#、 Sql Server、 html+css、 wpf、 wcf and so on

兢兢业业学技术 踏踏实实诚做人……
博客园    首页    新随笔    联系   管理    订阅  订阅
有关验证url 地址 和 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地址了 所以这种方法不严谨

徐燕平
posted on 2011-04-15 15:24  xyp0605  阅读(463)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3