The email addresses I want to validate are going to be used by an ASP.NET web application using the System.Net.Mail
namespace to send emails to a list of people.
So, rather than using some very complex regular expression, I just try to create a MailAddress
instance from the address. The MailAddress
constructor will throw an exception if the address is not formed properly. This way, I know I can at least get the email out of the door. Of course this is server-side validation, but at a minimum you need that anyway.
protected void emailValidator_ServerValidate(object source, ServerValidateEventArgs args)
{
try
{
var a = new MailAddress(txtEmail.Text);
}
catch (Exception ex)
{
args.IsValid = false;
emailValidator.ErrorMessage = "email: " + ex.Message;
}
}