当前位置: 代码迷 >> ASP.NET >> asp的正则表达式出错?求教!该如何处理
  详细解决方案

asp的正则表达式出错?求教!该如何处理

热度:9638   发布时间:2013-02-25 00:00:00.0
asp的正则表达式出错?!求教!
bool IsValidEmail(string strIn) // 验证邮箱
  { 
  // Return true if strIn is in valid e-mail format. 
  return Regex.IsMatch(strIn, @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* ");
  } 

.......

  if (!IsValidEmail(TextBox_email.Text))
  {
  Response.Write("<script language=javascript>alert('请输入正确的邮箱地址!');</script>");
  return;
  }

但是,我输进去的邮箱是对的也报错! 为什么?!

------解决方案--------------------------------------------------------
return Regex.IsMatch(strIn, @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");

你正则表达式最后面多了个空格
------解决方案--------------------------------------------------------
return Regex.IsMatch(strIn, @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* ");
红色部分多了一个空格,删除,改为
return Regex.IsMatch(strIn, @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
------解决方案--------------------------------------------------------

修改下
return Regex.IsMatch(strIn, @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
------解决方案--------------------------------------------------------
C# code
\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*