zoukankan      html  css  js  c++  java
  • 有关验证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地址了 所以这种方法不严谨

    徐燕平
  • 相关阅读:
    poj 1088 滑雪
    位运算与bitset
    hdu 4607 Park Visit
    树的直径
    codeforces 495D Sonya and Matrix
    German Collegiate Programming Contest 2015(第三场)
    BAPC 2014 Preliminary(第一场)
    Benelux Algorithm Programming Contest 2014 Final(第二场)
    E. Reachability from the Capital(tarjan+dfs)
    poj2104 K-th Number(划分树)
  • 原文地址:https://www.cnblogs.com/xyp0605/p/2017225.html
Copyright © 2011-2022 走看看