regular expression for email with one domain

Discussion in 'Javascript' started by ll, Feb 13, 2007.

  1. ll

    ll Guest

    I am trying to finalize a regular expression in javascript to only
    allow emails with a certain domain to be valid.

    Here is what I have so far:

    var emailFilter2=/[^\w\-\.]\@aol.com/;
    if(!(emailFilter2.test(strng))) {
    error = "Please enter a valid email address with the AOL domain.\n
    \n";
    }


    Thanks for any help with this.
    Louis
     
    ll, Feb 13, 2007
    #1
    1. Advertising

  2. ll

    Elegie Guest

    ll wrote:

    Hi,

    > I am trying to finalize a regular expression in javascript to only
    > allow emails with a certain domain to be valid.


    Unfortunately, email addresses cannot (probably) be tested with one
    single regexp, given the syntax options available in the relevant RFC.
    Moreover, a valid address, i.e. respecting the format defined in the
    specification, may simply not be receiving mail (it could be some fake
    address).

    Please check the following page, Appendix, Part 5, "Verifying Email
    Addresses", by Elijah Pogonatus. Also, pay attention to the two examples
    provided by the author.

    <URL:http://www.faqs.org/faqs/mail/addressing/>

    The thing is, it might simply be better to rethink your approach. If you
    accept only AOL email addresses, then why not use something like (along
    with some confirmation link sent to the address):

    ---
    Please enter your email address (AOL only).
    <input type="text" name="email">@aol.com
    ---

    Kind regards.
     
    Elegie, Feb 13, 2007
    #2
    1. Advertising

  3. ll

    Evertjan. Guest

    ll wrote on 13 feb 2007 in comp.lang.javascript:

    > I am trying to finalize a regular expression in javascript to only
    > allow emails with a certain domain to be valid.
    >
    > Here is what I have so far:
    >
    > var emailFilter2=/[^\w\-\.]\@aol.com/;


    The . after aol should be escaped.
    You should allow for case insensitivity.
    You should test for the end of string with $.

    > if(!(emailFilter2.test(strng))) {
    > error = "Please enter a valid email address with the AOL
    > domain.\n
    > \n";
    > }



    Far simpler, if you are only interested in testing the domain part:

    if (/.@aol\.com$/i.test(strng))
    .....

    If you want to test the part bedore the @,
    the problem is far greater.

    You would have to start with knowing the axact definition.

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Feb 13, 2007
    #3
  4. ll

    ll Guest

    > Far simpler, if you are only interested in testing the domain part:
    >
    > if (/.@aol\.com$/i.test(strng))
    > .....
    >


    Thanks for your reply,
    If I wanted to compare for two domains, would there be a way? I've
    tried this (below) but it fails to trap other domains?
    Thanks again
    Louis

    //
    if (!(/.@aol\.com$/i.test(strng))) || if (!(/.@yahoo\.com$/
    i.test(strng)))
    {
    error="Please enter a valid email address with either aol or yahoo
    domain.\n\n";
    }
     
    ll, Feb 14, 2007
    #4
  5. ll

    Evertjan. Guest

    ll wrote on 14 feb 2007 in comp.lang.javascript:

    >> Far simpler, if you are only interested in testing the domain part:
    >>
    >> if (/.@aol\.com$/i.test(strng))
    >> .....
    >>

    >
    > Thanks for your reply,
    > If I wanted to compare for two domains, would there be a way? I've
    > tried this (below) but it fails to trap other domains?
    >
    > //
    > if (!(/.@aol\.com$/i.test(strng))) || if (!(/.@yahoo\.com$/


    No you cannot "or" two if-s because they are statements.

    [read up on both javascript and regular expressions please]

    if (/.@(aol|yahoo)\.com$/i.test(emailStr))
    alert('One of those found!')



    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Feb 14, 2007
    #5
  6. ll

    Evertjan. Guest

    Michael White wrote on 14 feb 2007 in comp.lang.javascript:

    >> if (/.@(aol|yahoo)\.com$/i.test(emailStr))
    >> alert('One of those found!')

    >
    >
    > if (/.@(aol)|(yahoo)\.com$/i.test(emailStr))
    >
    > Aren't the parentheses needed to prevent matching
    > "aolahoo" and "aoyahoo" ?
    >


    did you test this?

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Feb 14, 2007
    #6
  7. In comp.lang.javascript message <45d23ce0$0$428$>,
    Tue, 13 Feb 2007 23:34:08, Elegie <> posted:
    >ll wrote:


    >> I am trying to finalize a regular expression in javascript to only
    >> allow emails with a certain domain to be valid.


    Try OK = /@some\.domain\.name$/.test(Str)

    >Unfortunately, email addresses cannot (probably) be tested with one
    >single regexp, given the syntax options available in the relevant RFC.


    That was not the question.

    >Moreover, a valid address, i.e. respecting the format defined in the
    >specification, may simply not be receiving mail (it could be some fake
    >address).


    Neither was that.

    Whatever the OP may have meant, the question requires that all mail not
    addressed to a specific domain be disallowed.

    All mail addressed to the domain which I am now using will have "merlyn"
    in the address; but so will mail to Joe.merlyn (*at) elsewhere.kom.

    Mail addressed to the domain will necessarily have a particular right
    part.

    One might attempt to address mail to the dotted quad - would that count
    as the domain?

    Mail addressed to my name in another domain will be redirected to my
    domain; the OP could allow mail to that other domain, but it redirects
    in many directions.

    --
    (c) John Stockton, Surrey, UK. REPLYyyww merlyn demon co uk Turnpike 6.05.
    Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
    Web <URL:http://www.merlyn.demon.co.uk/news-use.htm> : about usage of News.
    No Encoding. Quotes precede replies. Snip well. Write clearly. Mail no News.
     
    Dr J R Stockton, Feb 15, 2007
    #7
  8. ll

    Elegie Guest

    Dr J R Stockton wrote:

    Hi John,

    <snip>

    >> Unfortunately, email addresses cannot (probably) be tested with one
    >> single regexp, given the syntax options available in the relevant RFC.

    >
    > That was not the question.


    >> Moreover, a valid address, i.e. respecting the format defined in the
    >> specification, may simply not be receiving mail (it could be some fake
    >> address).

    >
    > Neither was that.


    Yes, the information I have provided did not answer to the exact
    question. However, I had the feeling that this original question had
    some issues itself, and that explaining these could help the OP into
    getting a better overview of his process. Still, offering a caveat
    without providing the straight answer was, as you suggested, probably
    some mistake from my side.

    Collecting email addresses generally exposes some will to use them in
    the future. A valid email address should not only respect a certain
    format, but should also reach its recipient. Unfortunately, most people
    tend to believe that testing for some particular format suffices (and to
    me the OQ was a potential expression of this syndrome), potentially
    building critical processes on unchecked data.

    Also, when designing an application, one should nearly never request the
    user to enter some input that is already predefined. Doing so can only
    confuse the user and slow down his inputting data.

    > Whatever the OP may have meant, the question requires that all mail not
    > addressed to a specific domain be disallowed.


    Which is why I proposed an alternate approach, not using a regular
    expression, but rather forcing the domain as plain text and simply
    requesting the local part of the address...

    > All mail addressed to the domain which I am now using will have "merlyn"
    > in the address; but so will mail to Joe.merlyn (*at) elsewhere.kom.
    >
    > Mail addressed to the domain will necessarily have a particular right
    > part.


    That is true, and most of the time people providing their email address
    would not complicate it using inappropriate subtleties, which should
    eventually make this 'right part' not too complicated to spot - though
    in regards of the possibilities permitted by the RFC, a regular
    expression alone is probably not enough to spot it safely.

    > One might attempt to address mail to the dotted quad - would that count
    > as the domain?


    If by "dotted quad" you refer to the IP address, then yes, that would
    indeed count as the domain.

    > Mail addressed to my name in another domain will be redirected to my
    > domain; the OP could allow mail to that other domain, but it redirects
    > in many directions.


    Quite an interesting point, but if some non AOL address redirects to the
    AOL one, then why not supply the AOL one in the first place? What if the
    user eventually switches email? Would this make him/her still a target?
    To which extent is the email a relevant criteria?


    Kind regards,
    Elegie.
     
    Elegie, Feb 16, 2007
    #8
  9. In comp.lang.javascript message <45d5084c$0$7259$>,
    Fri, 16 Feb 2007 02:26:30, Elegie <> posted:
    >
    >> Mail addressed to my name in another domain will be redirected to my
    >> domain; the OP could allow mail to that other domain, but it redirects
    >> in many directions.

    >
    >Quite an interesting point, but if some non AOL address redirects to
    >the AOL one, then why not supply the AOL one in the first place? What
    >if the user eventually switches email? Would this make him/her still a
    >target? To which extent is the email a relevant criteria?


    With my present setup, I only collect mail from Demon, and it is only my
    Demon address that I expose in News headers.

    I give my redirecting address only to trusted people who might want to
    use it after I have abandoned the Demon mail address, before which I
    shall re-aim the redirector. Then I will be receiving at a different
    domain, though the redirected mail will be sent as before to the
    redirecting domain.

    At one time, IIRC, a lower-grade professional organisation gave me
    redirection facilities, which I did not need or choose to use; I could
    have used it to redirect either to the Demon address or to the other
    redirector. They withdrew the facility when I pointed out that I was
    not a member.

    It is for such reasons that it is necessary for the original question to
    be asked with great care. Likewise, those who reply should be careful
    to indicate where they are answering the original question, and where
    they are providing other related advice.

    --
    (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
    Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
    Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
    Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
     
    Dr J R Stockton, Feb 16, 2007
    #9
  10. ll

    Evertjan. Guest

    Michael White wrote on 21 feb 2007 in comp.lang.javascript:

    > Evertjan. wrote:
    >> Michael White wrote on 14 feb 2007 in comp.lang.javascript:
    >>
    >>
    >>>>if (/.@(aol|yahoo)\.com$/i.test(emailStr))
    >>>> alert('One of those found!')
    >>>
    >>>
    >>>if (/.@(aol)|(yahoo)\.com$/i.test(emailStr))
    >>>
    >>>Aren't the parentheses needed to prevent matching
    >>>"aolahoo" and "aoyahoo" ?
    >>>

    >>
    >> did you test this?

    >
    > No, I didn't.


    Well, please do, Mick, and tell us your results.

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Feb 21, 2007
    #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,307
  2. =?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:
    851
    Alan Moore
    Dec 2, 2005
  3. GIMME
    Replies:
    3
    Views:
    11,974
    vforvikash
    Dec 29, 2008
  4. Replies:
    9
    Views:
    2,421
    Jussi Piitulainen
    Dec 2, 2006
  5. Sendhil
    Replies:
    0
    Views:
    233
    Sendhil
    Nov 12, 2003
Loading...

Share This Page