Monday, January 4, 2010

Email validation in C# window application

private void TextBox1_Validating(object sender, CancelEventArgs e)
{
System.Text.RegularExpressions.Regex rEmail = new System.Text.RegularExpressions.Regex(@"^[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]$");
if (txtEmail.Text.Length > 0)
{
if(!rEmail.IsMatch(txtEmail.Text))
{
MessageBox.Show("Please provide valid email address", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtEmail.SelectAll();
}
}
}

1 comment:

darichkid said...

This is an excellent component for verifying email addresses:
http://www.kellermansoftware.com/p-37-net-email-validation.aspx