Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate email address against invalid characters

Tags:

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. å[email protected]). 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?

like image 577
hejto Avatar asked May 25 '16 10:05

hejto


1 Answers

Those characters are not invalid. Unusual, but not invalid. The question you linked even contains an explanation why you shouldn't care.

Full use of electronic mail throughout the world requires that (subject to other constraints) people be able to use close variations on their own names (written correctly in their own languages and scripts) as mailbox names in email addresses.

- RFC 6530, 2012

like image 103
Manfred Radlwimmer Avatar answered Nov 03 '22 01:11

Manfred Radlwimmer