您的位置:首页 > 其它

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

2012-08-30 14:26 211 查看
前一段时间,在项目中做了比较多的正则验证,现在来把常见的总结一下:

手机号码格式的验证:

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+"电话号码,请重新输入!");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐