手机,网址和电话号码的正则表达式验证

 前一段时间,在项目中做了比较多的正则验证,现在来把常见的总结一下:

手机号码格式的验证:

Regex rg = new Regex(@"^0?(13[0-9]|15[012356789]|18[0236789]|14[57])[0-9]{8}$");
            string mobile = dto.Mobile.ToString().Trim();
            Match m = rg.Match(mobile);
            if (!m.Success)
            {
                return Error("返回", "您好,您所输入的手机号码格式不正确,请重新选择输入!");
            }

 

//在企业简介里面过滤网站地址          

           string description = dto.Description.ToString().Trim();
           Regex url = new Regex(@"(?i)(http://https//)?(\w+\.){1,3}(com(\.cn)?|cn|net|info|org|us|tk)\b", RegexOptions.IgnoreCase);           

           Match m = url.Match(description);
            int matchNum = 0;
            while (m.Success)
            {
                ++matchNum;
                m = m.NextMatch();
            }
            if (matchNum > 0)
            {
                return Error("返回", "您好,您所输入的信息包含个" + matchNum + "个网址,请重新输入!");
            }

 

//在企业简介里面过滤电话号码
            Regex telphoneno = new Regex(@"(\(\d{3,4}\)|\d{3,4}-)?\d{8}",RegexOptions.IgnoreCase);
            Match s = telphoneno.Match(description);
            int matchCount = 0;
            while(s.Success)
            {
                ++matchCount;
                s = s.NextMatch();
            }
            if (matchCount>0)
            {
                return Error("返回", "您好,您所输入的信息包含个"+matchCount+"电话号码,请重新输入!");
            }

 

posted @ 2012-08-30 14:26  美梦成真  阅读(308)  评论(0编辑  收藏  举报