tags:

views:

32

answers:

3

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???

A: 

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

Dieter G
Nice answer...thanks a lot!!!
Srivastava
A: 

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
I cannot help but notice this fails to cover all cases (as most regex cobbled towards for such purposes does).
Rushyo
A: 

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