1

I have used regular expression in email field validation . But after i used session and authentication in my website,none of the regular expression is working for me.

Can anybody help me out of this???

Srivastava
  • 3,500
  • 12
  • 46
  • 74

3 Answers3

0

Hard to answer your question with no code.

But personally I would not use a regex to validate an email address. See this question

I like this method:

protected void emailValidator_ServerValidate(object source, ServerValidateEventArgs args)
{
    try
    {
        var a = new MailAddress(txtEmail.Text);
    }
    catch (Exception ex)
    {
        args.IsValid = false;
        emailValidator.ErrorMessage = "email: " + ex.Message;
    }
}

This might also be educational: I Knew How To Validate An Email Address Until I Read The RFC

Community
  • 1
  • 1
Dieter G
  • 930
  • 2
  • 9
  • 24
0

try this code under regularexpression validator control of asp.net----> ^[_a-zA-Z0-9-]+(.[_a-zA-Z0-9-]+)@[a-zA-Z0-9-]+(.[a-zA-Z0-9-]+).(([0-9]{1,3})|([a-zA-Z]{2,3})|(aero|coop|info|museum|name))$

preety
  • 1,016
  • 3
  • 12
  • 18
  • I cannot help but notice this fails to cover all cases (as most regex cobbled towards for such purposes does). – Rushyo Sep 17 '10 at 10:11
0

The best validation method is to send a confirmation email.

This article is a good intro on why:

http://www.regular-expressions.info/email.html

And if you really want an RFC2822 regexp, here it is:

(?:[a-z0-9!#$%&'+/=?^_{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_{|}~-]+)|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\[\x01-\x09\x0b\x0c\x0e-\x7f])")@(?:(?:a-z0-9?.)+a-z0-9?|[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-][a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\[\x01-\x09\x0b\x0c\x0e-\x7f])+)])

Tom Gullen
  • 61,249
  • 84
  • 283
  • 456