In validating email addresses I have tried using both the EmailAddressAttribute
class from System.ComponentModel.DataAnnotations
:
[EmailAddress(ErrorMessage = "Invalid Email Address")]
public string Email { get; set; }
and the MailAddress
class from System.Net.Mail
by doing:
bool IsValidEmail(string email)
{
try {
var addr = new System.Net.Mail.MailAddress(email);
return addr.Address == email;
}
catch {
return false;
}
}
as suggested in C# code to validate email address. Both methods work in principle, they catch invalid email addresses like, e.g., user@
, not fulfilling the format user@host
.
My problem is that none of the two methods detect invalid characters in the user field, such as æ, ø, or å (e.g. åge@gmail.com)
. Is there any reason for why such characters are not returning a validation error? And do anybody have a elegant solution on how to incorporate a validation for invalid characters in the user field?