0

I am new to c# and Ive come across something I can't fix.

situation

I am creating a windows form application using visual studio 2010 which has the option to validate an email address. I looked at tutorials on using RegularExpressions and have came to this. I feel I'm missing something because every time I validate, it only sends the MessageBox.Show("Email invalid"); to the user.

code

private void validateBtn_Click(object sender, EventArgs e)
{
    Regex email = new Regex("^[a-zA-Z0-9]{1-20}@[a-zA-Z0-9]{1-20}.[a-zA-Z]{2-3}$");

    if (!email.IsMatch(emailTxt.Text))
    {
        MessageBox.Show("Email invalid");
    }
    else
        MessageBox.Show("Email Valid");
}
Javran
  • 3,394
  • 2
  • 23
  • 40
  • having the text you are tryint to validate would help – Felice Pollano Apr 15 '14 at 15:02
  • What is the email address that you're entering that it fails on? Your regex has couple of issues. One is that `.` is a special character that means "any character"; you're using it as literal and should be escaped like `\.` (but that would not cause failure. Second issue that I see is that it will not handle addresses like `one@two.three.com`. But give us your input so we can help better. – LB2 Apr 15 '14 at 15:03
  • 2
    Obligatory link for anyone who's trying to [use a regular expression to validate an email address](http://stackoverflow.com/a/201378/1336590). – Corak Apr 15 '14 at 15:05

3 Answers3

2

Normally I would never use an Exception to direct program logic like this, but when validating emails I've found using built-in .NET functionality to be more reliable and readable than trying to use Regex to validate an email address.

using (var mm = new MailMessage())
{
    try
    {
        mm.To.Add(emailTxt.Text);
        MessageBox.Show("Email Valid");

        ... rest of your code to send the email
    }
    catch (FormatException)
    {
        MessageBox.Show("Email Invalid");
    }
}

Alternatively, to just test the address:

try
{
    var mail = new MailAddress(emailTxt.Text);
    MessageBox.Show("Email Valid");
}
catch (FormatException)
{
    MessageBox.Show("Email Invalid");
}

This class is not perfect, and you can find posts showing it doesn't catch every edge case.

But when this is the alternative, I think I'd rather stick with the above and take my chances.

Community
  • 1
  • 1
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
0

. is a special character, use

Regex email = new Regex(@"^[a-zA-Z0-9]{1-20}@[a-zA-Z0-9]{1-20}\.[a-zA-Z]{2-3}$");

please note I added a '@' in front of the string to avoid escaping the '\'

Felice Pollano
  • 32,832
  • 9
  • 75
  • 115
  • That likely isn't the root cause of the issue (we don't have OP's input data to know for sure), since `.` special character will actually match `.` literal and wouldn't cause failure. – LB2 Apr 15 '14 at 15:04
0

You have two syntax errors in your regex:

. stands for a random sign, so it must be escaped with a backslash: \. .

{1-20} musst be written with comma: {1,20}.

[a-zA-Z0-9]{1,20}@[a-zA-Z0-9]{1,20}\.[a-zA-Z]{2,3} 

works.

if you use C# you have to bring a @ before the first " to say it that it should not use backslashes in the string to escape. It should do this when searching for regex.

@"[a-zA-Z0-9]{1,20}@[a-zA-Z0-9]{1,20}\.[a-zA-Z]{2,3} "