I have the following code for Email ID validation. I am using the Regular expression to do it. However the following email Id is coming as invalid.
test_@gmail.com
What should be changed in the Regular expression so it passes the email Id (test_@gmail.com
)
public static bool IsValidEmail(string strIn)
{
invalid = false;
if (String.IsNullOrEmpty(strIn))
return false;
// Use IdnMapping class to convert Unicode domain names.
try
{
strIn = Regex.Replace(strIn, @"(@)(.+)$", DomainMapper);
}
catch (Exception e)
{
Utility.writeToLogFile(String.Format("Error while validating the Email Address {0}. Error Code.e", strIn.ToString(), e));
return false;
}
if (invalid)
return false;
// Return true if strIn is in valid e-mail format.
try
{
return Regex.IsMatch(strIn,
@"^(?("")(""[^""]+?""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
@"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9]{2,17}))$",
RegexOptions.IgnoreCase);
}
catch (Exception e)
{
Utility.writeToLogFile(String.Format("Error while validating the Email Address {0}. Error Code.e", strIn.ToString(), e));
return false;
}
}