2

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.

Community
  • 1
  • 1
HasanG
  • 12,734
  • 29
  • 100
  • 154

3 Answers3

4

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: '.'."
}
Oleks
  • 31,955
  • 11
  • 77
  • 132
3

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.

m.edmondson
  • 30,382
  • 27
  • 123
  • 206
3

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

vinayvasyani
  • 1,393
  • 4
  • 13
  • 34