Regular Expression for Email Validation

Discussion in 'ASP .Net Web Controls' started by jmhmaine, Jan 24, 2005.

  1. jmhmaine

    jmhmaine Guest

    I use the following Regular Expression to check the format of the text in a
    textbox when requesting an email:

    ValidationExpression="^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"

    It works great, but I just discovered a bug if someone provides a single
    character before the @ sign. The following doesn't pass:



    Is there a quick fix or better Expression to use? Thanks.
     
    jmhmaine, Jan 24, 2005
    #1
    1. Advertising

  2. jmhmaine

    Mike Ryan Guest

    Hello,

    If you haven't already, you might try:

    http://www.regexlib.com/

    - Mike

    jmhmaine wrote:
    > I use the following Regular Expression to check the format of the text in a
    > textbox when requesting an email:
    >
    > ValidationExpression="^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"
    >
    > It works great, but I just discovered a bug if someone provides a single
    > character before the @ sign. The following doesn't pass:
    >
    >
    >
    > Is there a quick fix or better Expression to use? Thanks.
     
    Mike Ryan, Jan 24, 2005
    #2
    1. Advertising

  3. Hi Jmhmaine,

    I also think www.regexlib.com is a good place to find some exsiting
    sophisticate regex.
    In addition, since it would be a bit hard to modify an existing complex
    regex just to fit some small requirement, I suggest you consider change the
    validtion logic of your control into two case test. For example, in your
    validation control's checking code,

    first validate the input through the regex test , then if failed, check the
    particular " a single character before the @ sign" scenario. How do you
    think of this?

    Thanks.

    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    Steven Cheng[MSFT], Jan 25, 2005
    #3
  4. I am not sure you want to be using \w, because it is equivalent to
    [a-zA-Z_0-9], which includes underscores. I don't think that underscores are
    allowed in either mailbox or domain names, and your regular expression
    allows first_last@subdomain_domain.com. I think that mailbox names must be
    alphanumeric letter clusters separated by either periods or hyphens, (not
    both), and domain names must be clusters of alphanumerics separated by
    hyphens, separated by periods. Your regular expression excludes
    and allows .

    I have a really long, ugly one that I am using, but if anyone can improve
    it, or knows of a mail server that allows mailbox names with underscores, I
    would appreciate being corrected. My regular expression limits the domain
    type to 2-4 alpha characters. I have not heard of any longer than 4, i.e.,
    ".info", but if anyone knows of longer legal ones, please speak up

    My regular expression is,

    @"^(((([0-9A-Za-z]+(\-[0-9A-Za-z]+)*)|([0-9A-Za-z]+(\.[0-9A-Za-z]+)*))@([0-9
    A-Za-z]+([-][0-9A-Za-z]+)*)(\.([0-9A-Za-z]+([-][0-9A-Za-z]+)*))*\.[a-zA-Z]{2
    ,4}))$";

    "jmhmaine" <> wrote in message
    news:D...
    > I use the following Regular Expression to check the format of the text in

    a
    > textbox when requesting an email:
    >
    >

    ValidationExpression="^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-
    zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"
    >
    > It works great, but I just discovered a bug if someone provides a single
    > character before the @ sign. The following doesn't pass:
    >
    >
    >
    > Is there a quick fix or better Expression to use? Thanks.
     
    David Alexander, Jan 25, 2005
    #4
  5. jmhmaine

    jmhmaine Guest

    Good points, I didn't create this expression, I found it a year ago as a
    sample. A few comments on your points:

    There is a new top level domain .museum
    http://musedoma.museum/
    So you should allow up to 7 characters.

    I believe I have seen _ underscores in emails before, not sure if legal per
    RFC. I jusr ran a test sending a email and Outlook
    2003 sent it and my ISP forwarded it back to my POP3.

    "David Alexander" wrote:

    > I am not sure you want to be using \w, because it is equivalent to
    > [a-zA-Z_0-9], which includes underscores. I don't think that underscores are
    > allowed in either mailbox or domain names, and your regular expression
    > allows first_last@subdomain_domain.com. I think that mailbox names must be
    > alphanumeric letter clusters separated by either periods or hyphens, (not
    > both), and domain names must be clusters of alphanumerics separated by
    > hyphens, separated by periods. Your regular expression excludes
    > and allows .
    >
    > I have a really long, ugly one that I am using, but if anyone can improve
    > it, or knows of a mail server that allows mailbox names with underscores, I
    > would appreciate being corrected. My regular expression limits the domain
    > type to 2-4 alpha characters. I have not heard of any longer than 4, i.e.,
    > ".info", but if anyone knows of longer legal ones, please speak up
    >
    > My regular expression is,
    >
    > @"^(((([0-9A-Za-z]+(\-[0-9A-Za-z]+)*)|([0-9A-Za-z]+(\.[0-9A-Za-z]+)*))@([0-9
    > A-Za-z]+([-][0-9A-Za-z]+)*)(\.([0-9A-Za-z]+([-][0-9A-Za-z]+)*))*\.[a-zA-Z]{2
    > ,4}))$";
    >
    > "jmhmaine" <> wrote in message
    > news:D...
    > > I use the following Regular Expression to check the format of the text in

    > a
    > > textbox when requesting an email:
    > >
    > >

    > ValidationExpression="^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-
    > zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"
    > >
    > > It works great, but I just discovered a bug if someone provides a single
    > > character before the @ sign. The following doesn't pass:
    > >
    > >
    > >
    > > Is there a quick fix or better Expression to use? Thanks.

    >
    >
    >
     
    jmhmaine, Jan 27, 2005
    #5
  6. jmhmaine

    jmhmaine Guest

    I would like use URL:
    http://www.regexlib.com/REDetails.aspx?regexp_id=711

    But how do incorporate the expression:
    ^((?>[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+\x20*|"((?=[\x01-\x7f])[
    ^"\\]|\\[\x01-\x7f])*"\x20*)*(?<angle><))?((?!\.)(?>\.?[a-zA
    -Z\d!#$%&'*+\-/=?^_`{|}~]+)+|"((?=[\x01-\x7f])[^"\\]|\\[\x01
    -\x7f])*")@(((?!-)[a-zA-Z\d\-]+(?<!-)\.)+[a-zA-Z]{2,}|\[(((?
    (?<!\[)\.)(25[0-5]|2[0-4]\d|[01]?\d?\d)){4}|[a-zA-Z\d\-]*[a-
    zA-Z\d]:((?=[\x01-\x7f])[^\\\[\]]|\\[\x01-\x7f])+)\])(?(angl
    e)>)$

    Into the ValidationExpression statement? The problem is that this expression
    contains both single and double quotes, so I can't assign the it to the
    ValidationExpression attribute. Is there a way to escape within ASPX Web
    Controls statements? Thanks.

    "Steven Cheng[MSFT]" wrote:

    > Hi Jmhmaine,
    >
    > I also think www.regexlib.com is a good place to find some exsiting
    > sophisticate regex.
    > In addition, since it would be a bit hard to modify an existing complex
    > regex just to fit some small requirement, I suggest you consider change the
    > validtion logic of your control into two case test. For example, in your
    > validation control's checking code,
    >
    > first validate the input through the regex test , then if failed, check the
    > particular " a single character before the @ sign" scenario. How do you
    > think of this?
    >
    > Thanks.
    >
    > Regards,
    >
    > Steven Cheng
    > Microsoft Online Support
    >
    > Get Secure! www.microsoft.com/security
    > (This posting is provided "AS IS", with no warranties, and confers no
    > rights.)
    >
    >
     
    jmhmaine, Jan 27, 2005
    #6
  7. Hi Jmhmaine,

    Thanks for your followup. I think the problem is caused by the clientside
    script has different escape char for single and double quote from the
    serverside .net code.

    In dot.net string we can use \" \' to replace all the " and ' , but this
    will be incorrectly escaped by the clientside browser when parsing this
    expression.

    Currently I think we may need to use custom validation control for such
    complex regex scenario, manualy generate the regular expression for
    serverside .net code and clientside script code separately.

    Thanks.

    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    Steven Cheng[MSFT], Jan 27, 2005
    #7
  8. jmhmaine

    jmhmaine Guest

    The problem I have is an ASP.NET compile error because I can't assign the
    expression.

    I tried with double quotes:
    <asp:RegularExpressionValidator ID="valRegExprEmail"
    Runat="server"
    ControlToValidate="txtEmail"
    ValidationExpression="^((?>[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+\x20*|"((?=[\x01-\x7f])[^"\\]|\\[\x01-\x7f])*"\x20*)*(?<angle><))?((?!\.)(?>\.?[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+)+|"((?=[\x01-\x7f])[^"\\]|\\[\x01-\x7f])*")@(((?!-)[a-zA-Z\d\-]+(?<!-)\.)+[a-zA-Z]{2,}|\[(((?(?<!\[)\.)(25[0-5]|2[0-4]\d|[01]?\d?\d)){4}|[a-zA-Z\d\-]*[a-zA-Z\d]:((?=[\x01-\x7f])[^\\\[\]]|\\[\x01-\x7f])+)\])(?(angl
    e)>)$"
    ErrorMessage="The email address provided is not formatted correctly."
    Display="None" />

    And Single Quotes:
    <asp:RegularExpressionValidator ID="valRegExprEmail"
    Runat="server"
    ControlToValidate="txtEmail"
    ValidationExpression='^((?>[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+\x20*|"((?=[\x01-\x7f])[^"\\]|\\[\x01-\x7f])*"\x20*)*(?<angle><))?((?!\.)(?>\.?[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+)+|"((?=[\x01-\x7f])[^"\\]|\\[\x01-\x7f])*")@(((?!-)[a-zA-Z\d\-]+(?<!-)\.)+[a-zA-Z]{2,}|\[(((?(?<!\[)\.)(25[0-5]|2[0-4]\d|[01]?\d?\d)){4}|[a-zA-Z\d\-]*[a-zA-Z\d]:((?=[\x01-\x7f])[^\\\[\]]|\\[\x01-\x7f])+)\])(?(angl
    e)>)$'
    ErrorMessage="The email address provided is not formatted correctly."
    Display="None" />


    "Steven Cheng[MSFT]" wrote:

    > Hi Jmhmaine,
    >
    > Thanks for your followup. I think the problem is caused by the clientside
    > script has different escape char for single and double quote from the
    > serverside .net code.
    >
    > In dot.net string we can use \" \' to replace all the " and ' , but this
    > will be incorrectly escaped by the clientside browser when parsing this
    > expression.
    >
    > Currently I think we may need to use custom validation control for such
    > complex regex scenario, manualy generate the regular expression for
    > serverside .net code and clientside script code separately.
    >
    > Thanks.
    >
    > Regards,
    >
    > Steven Cheng
    > Microsoft Online Support
    >
    > Get Secure! www.microsoft.com/security
    > (This posting is provided "AS IS", with no warranties, and confers no
    > rights.)
    >
    >
    >
    >
    >
     
    jmhmaine, Jan 27, 2005
    #8
  9. Since underscores are supported by some mail programs, I will revise my
    regular expression to allow them. Thanks for letting me know. I spent a fair
    amount of time reading RFC 821
    (http://www.networksorcery.com/enp/rfc/rfc821.txt) which claims that only
    alphanumerics and periods are allowed. The version I looked at was written
    in 1982, though. Current practice probably differs a lot.

    As a mail server admin, I wouldn't allow underscores, though, because so
    many programs (like Outlook) display email addresses formatted as underlined
    links. Except for technical people who are familiar with syntactic
    requirements, a lot of people would end up thinking the underscore was a
    space. (I still talk to people who ask me "No space?" when I am providing a
    website URL or email address over the phone.)

    "jmhmaine" <> wrote in message
    news:...
    > Good points, I didn't create this expression, I found it a year ago as a
    > sample. A few comments on your points:
    >
    > There is a new top level domain .museum
    > http://musedoma.museum/
    > So you should allow up to 7 characters.
    >
    > I believe I have seen _ underscores in emails before, not sure if legal

    per
    > RFC. I jusr ran a test sending a email and Outlook
    > 2003 sent it and my ISP forwarded it back to my POP3.
    >
    > "David Alexander" wrote:
    >
    > > I am not sure you want to be using \w, because it is equivalent to
    > > [a-zA-Z_0-9], which includes underscores. I don't think that underscores

    are
    > > allowed in either mailbox or domain names, and your regular expression
    > > allows first_last@subdomain_domain.com. I think that mailbox names must

    be
    > > alphanumeric letter clusters separated by either periods or hyphens,

    (not
    > > both), and domain names must be clusters of alphanumerics separated by
    > > hyphens, separated by periods. Your regular expression excludes
    > > and allows .
    > >
    > > I have a really long, ugly one that I am using, but if anyone can

    improve
    > > it, or knows of a mail server that allows mailbox names with

    underscores, I
    > > would appreciate being corrected. My regular expression limits the

    domain
    > > type to 2-4 alpha characters. I have not heard of any longer than 4,

    i.e.,
    > > ".info", but if anyone knows of longer legal ones, please speak up
    > >
    > > My regular expression is,
    > >
    > >

    @"^(((([0-9A-Za-z]+(\-[0-9A-Za-z]+)*)|([0-9A-Za-z]+(\.[0-9A-Za-z]+)*))@([0-9
    > >

    A-Za-z]+([-][0-9A-Za-z]+)*)(\.([0-9A-Za-z]+([-][0-9A-Za-z]+)*))*\.[a-zA-Z]{2
    > > ,4}))$";
    > >
    > > "jmhmaine" <> wrote in message
    > > news:D...
    > > > I use the following Regular Expression to check the format of the text

    in
    > > a
    > > > textbox when requesting an email:
    > > >
    > > >

    > >

    ValidationExpression="^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-
    > > zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"
    > > >
    > > > It works great, but I just discovered a bug if someone provides a

    single
    > > > character before the @ sign. The following doesn't pass:
    > > >
    > > >
    > > >
    > > > Is there a quick fix or better Expression to use? Thanks.

    > >
    > >
    > >
     
    David Alexander, Jan 27, 2005
    #9
  10. Hi Jmhmaine,

    Thanks for your followup. I think the compile error is also due to the
    compile system incorrectly escape such a complex expression when we set it
    in the aspx page inline. Currently what I do is assign the
    validtionExpression in code behind and escaping all the single , double
    quote and back slash. For example:

    private void Page_Load(object sender, System.EventArgs e)
    {
    rvEmail.ValidationExpression =
    "^((?>[a-zA-Z\\d!#$%&\'*+\\-/=?^_`{|}~]+\\x20*|\"((?=[\\x01-\\x7f])[^\"\\\\]
    |\\\\[\\x01-\\x7f])*\"\\x20*)*(?<angle><))?((?!\\.)(?>\\.?[a-zA-Z\\d!#$%&\'*
    +\\-/=?^_`{|}~]+)+|\"((?=[\\x01-\\x7f])[^\"\\\\]|\\\\[\\x01-\\x7f])*\")@(((?
    !-)[a-zA-Z\\d\\-]+(?<!-)\\.)+[a-zA-Z]{2,}|\\[(((?(?<!\\[)\\.)(25[0-5]|2[0-4]
    \\d|[01]?\\d?\\d)){4}|[a-zA-Z\\d\\-]*[a-zA-Z\\d]:((?=[\\x01-\\x7f])[^\\\\\\[
    \\]]|\\\\[\\x01-\\x7f])+)\\])(?(angle)>)$";

    }

    But we still can't make both the clientside and serverside work together,
    need to turn off the clientside validation.
    Hope this helps.

    Thanks & Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    Steven Cheng[MSFT], Jan 28, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,382
  2. Linus Nikander

    Email regular expression validator

    Linus Nikander, Sep 19, 2003, in forum: Java
    Replies:
    2
    Views:
    12,263
    Roedy Green
    Sep 19, 2003
  3. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    883
    Alan Moore
    Dec 2, 2005
  4. GIMME
    Replies:
    3
    Views:
    12,048
    vforvikash
    Dec 29, 2008
  5. Clive

    EMail Validation (Regular Expression)

    Clive, Sep 22, 2003, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    145
    vMike
    Sep 22, 2003
Loading...

Share This Page