Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Internet e-mail validation expression validates everything

Tags:

regex

asp.net

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.

like image 741
HasanG Avatar asked Aug 25 '26 20:08

HasanG


1 Answers

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: '.'."
}
like image 83
Oleks Avatar answered Aug 28 '26 11:08

Oleks