【发布时间】:2018-11-21 09:37:44
【问题描述】:
我有一个字段Company profile: 文本框
如果用户在文本框中输入任何 emailid,验证错误消息应显示用户无法在文本框中输入 emailid。
我已经尝试了以下代码:
Regex regex = new Regex(@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
string[] values = commentstxt.Text.Trim().Split(' ');
for (int i = 0; i < values.Length; i++)
{
bool isValid = regex.IsMatch(values[i].ToString().Trim());
if (isValid)
{
//ScriptManager.RegisterStartupScript(this, this.GetType(), "CropImage", "alert('you can not enter email id.');", true);
//break;
Response.Write("<script language='javascript'>window.alert('you can not enter email id in company profile.');window.location='addlisting.aspx';</script>");
break;
}
else
{
Server.Transfer("addlistingpost.aspx", true);
}
}
如果用户只输入test@gmail.com,它会给出验证消息说你不能在文本框中输入正确的emailid,并停留在addlisting.aspx页面中。
如果用户输入hello..how are you,它会重定向到addlistingpost.aspx,这也是正确的。
当用户输入 hello test@gmail.com how are you 时出现问题,它不会抛出验证消息,因为文本框中存在 emailid。我知道这里只是比较values[0],也就是hello,然后直接进入else部分。
如何做到这一点?
【问题讨论】:
-
什么是“emailids”?
-
另外,您使用的正则表达式与合法电子邮件地址的 RFC 5322 规范不匹配。
标签: c# asp.net validation