I always use the below regular expression to validate the email address. This is the best regex I have ever seen to validate email address.
"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z";
This regular expression I always uses in my Asp.NET Code and I'm pretty satisfied with it.
use this assembly reference
using System.Text.RegularExpressions;
and try the following code, as it is simple and do the work for you.
private bool IsValidEmail(string email) {
bool isValid = false;
const string pattern = @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z";
isValid = email != "" && Regex.IsMatch(email, pattern);
// an alternative of the above line is also given and commented
//
//if (email == "") {
// isValid = false;
//} else {
// // address provided so use the IsMatch Method
// // of the Regular Expression object
// isValid = Regex.IsMatch(email, pattern);
//}
return isValid;
}
this function validates the email string. If the email string is null, it returns false, if the email string is not in a correct format it returns false. It only returns true if the format of the email is valid.