在C#窗口窗体中,可以如何实现用于电子邮件地址的MaskedTextBox
public void Form1_Load(Object sender, EventArgs e)
{
// Other initialization code
mtxtEmailID.Mask = "..........";
Mask Type代替点应该是什么
mtxtEmailID.MaskInputRejected += new MaskInputRejectedEventHandler(mtxtEmailID_MaskInputRejected)
}
void mtxtEmailID_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
{
if(!Regex.IsMatch(txtEmailID.Text, "^[a-zA-Z][w.-]*[a-zA-Z0-9]@[a-zA-Z0-9][w.-]*[a-zA-Z0-9].[a-zA-Z][a-zA-Z.]*[a-zA-Z]$"))
这里的正则表达式给了我错误,让我知道什么是正确的电子邮件验证。
{
toolTip1.ToolTipTitle = "Invalid Input";
toolTip1.Show("Enter valid email address", mtxtEMailID);
}
}
你可以在这里找到关于MaskedTextBox的信息
如果你想验证电子邮件地址正则表达式不是正确的选择。 regex
不会覆盖很多角落案例...
使用MailAddress
try
{
address = new MailAddress(address).Address;
//email address is valid since the above line has not thrown an exception
}
catch(FormatException)
{
//address is invalid
}
但是,如果你需要正则表达式,它应该是:
.+@.+
我的项目中的这种方法使得电子邮件验证变得简单,仅考虑了诸如“@”和“。”之类的电子邮件中重要的几个因素。 。 我觉得不要让它变得复杂,因为每个人的电子邮件地址都不是强制性的。
private void txtEmailID_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
string errorMsg;
if (!ValidEmailAddress(txtEmailID.Text, out errorMsg))
{
// Cancel the event and select the text to be corrected by the user.
e.Cancel = true;
txtEmailID.Select(0, txtEmailID.Text.Length);
// Set the ErrorProvider error with the text to display.
this.errorProvider1.SetError(txtEmailID, errorMsg);
}
}
public bool ValidEmailAddress(string txtEmailID, out string errorMsg)
{
// Confirm that the e-mail address string is not empty.
if (txtEmailID.Length == 0)
{
errorMsg = "e-mail address is required.";
return false;
}
// Confirm that there is an "@" and a "." in the e-mail address, and in the correct order.
if (txtEmailID.IndexOf("@") > -1)
{
if (txtEmailID.IndexOf(".", txtEmailID.IndexOf("@")) > txtEmailID.IndexOf("@"))
{
errorMsg = "";
return true;
}
}
errorMsg = "e-mail address must be valid e-mail address format.n" +
"For example 'someone@example.com' ";
return false;
}
private void txtEmailID_Validated(object sender, EventArgs e)
{
// If all conditions have been met, clear the ErrorProvider of errors.
errorProvider1.SetError(txtEmailID, "");
}
链接地址: http://www.djcxy.com/p/92845.html
上一篇: In C# windows forms how a MaskedTextBox for email address can be implemented