HasanG
HasanG

Reputation: 13161

Internet e-mail validation expression validates everything

I'm validating input in a asp.net page but the problem is it validates e-mails like hasangürsoy@şşıı.com

My code is:

if (Regex.IsMatch(email, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"))
{ valid }
else { invalid }

EDIT: I've written a question before especially to validate e-mail addresses including Turkish characters but now I don't want users to be able to input mails with Turkish characters because users mostly type Turkish characters by mistake and I cannot send mails to these addresses.

Upvotes: 2

Views: 686

Answers (3)

Oleks
Oleks

Reputation: 32333

Why don't you just use build-in System.Net.Mail.MailAddress class for email validation?

bool isValidEmail = false;
try
{
    var email = new MailAddress("hasangürsoy@şşıı.com");
    isValidEmail = true;
{
catch (FormatException x)
{
    // gets "An invalid character was found in the mail header: '.'."
}

Upvotes: 4

vinayvasyani
vinayvasyani

Reputation: 1429

I recommend you reading this: http://www.codeproject.com/KB/validation/Valid_Email_Addresses.aspx

Upvotes: 3

m.edmondson
m.edmondson

Reputation: 30892

RFC3692 goes into great detail about how to properly validate e-mail addresses, which currently only correctly handle ASCII characters. However this is due to change, and hence your validation should be as relaxed as possible.

I would likely use an expression such as:

.+@.+

which would ensure you're not turning people away because your regular expression gives them no choice.

If the e-mail is important you should be following it up with verification usually done via sending an e-mail to the supplied address containing a link.

Upvotes: 3

Related Questions