regular expression for email with one domain

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ll

    regular expression for email with one domain

    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(!(emailFilte r2.test(strng)) ) {
    error = "Please enter a valid email address with the AOL domain.\n
    \n";
    }


    Thanks for any help with this.
    Louis

  • Elegie

    #2
    Re: regular expression for email with one domain

    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">@a ol.com
    ---

    Kind regards.

    Comment

    • Evertjan.

      #3
      Re: regular expression for email with one domain

      ll wrote on 13 feb 2007 in comp.lang.javas cript:
      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(!(emailFilte r2.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)

      Comment

      • ll

        #4
        Re: regular expression for email with one domain

        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";
        }

        Comment

        • Evertjan.

          #5
          Re: regular expression for email with one domain

          ll wrote on 14 feb 2007 in comp.lang.javas cript:
          >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)

          Comment

          • Michael White

            #6
            Re: regular expression for email with one domain

            Evertjan. wrote:
            ll wrote on 14 feb 2007 in comp.lang.javas cript:
            >
            >
            >>>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!')

            if (/.@(aol)|(yahoo) \.com$/i.test(emailStr ))

            Aren't the parentheses needed to prevent matching
            "aolahoo" and "aoyahoo" ?

            Mick

            Comment

            • Evertjan.

              #7
              Re: regular expression for email with one domain

              Michael White wrote on 14 feb 2007 in comp.lang.javas cript:
              >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)

              Comment

              • Dr J R Stockton

                #8
                Re: regular expression for email with one domain

                In comp.lang.javas cript message <45d23ce0$0$428 $426a74cc@news. free.fr>,
                Tue, 13 Feb 2007 23:34:08, Elegie <elegie@invalid .composted:
                >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)
                >Unfortunatel y, 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
                >specificatio n, 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.demo n.co.uk/news-use.htm: about usage of News.
                No Encoding. Quotes precede replies. Snip well. Write clearly. Mail no News.

                Comment

                • Elegie

                  #9
                  Re: regular expression for email with one domain

                  Dr J R Stockton wrote:

                  Hi John,

                  <snip>
                  >Unfortunatel y, 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
                  >specificatio n, 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.

                  Comment

                  • Dr J R Stockton

                    #10
                    Re: regular expression for email with one domain

                    In comp.lang.javas cript message <45d5084c$0$725 9$426a74cc@news .free.fr>,
                    Fri, 16 Feb 2007 02:26:30, Elegie <elegie@invalid .composted:
                    >
                    >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.demo n.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)

                    Comment

                    • Michael White

                      #11
                      Re: regular expression for email with one domain

                      Evertjan. wrote:
                      Michael White wrote on 14 feb 2007 in comp.lang.javas cript:
                      >
                      >
                      >>>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.
                      Mick

                      Comment

                      • Evertjan.

                        #12
                        Re: regular expression for email with one domain

                        Michael White wrote on 21 feb 2007 in comp.lang.javas cript:
                        Evertjan. wrote:
                        >Michael White wrote on 14 feb 2007 in comp.lang.javas cript:
                        >>
                        >>
                        >>>>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)

                        Comment

                        • Michael White

                          #13
                          Re: regular expression for email with one domain

                          Evertjan. wrote:
                          Michael White wrote on 21 feb 2007 in comp.lang.javas cript:
                          >
                          >
                          >>Evertjan. wrote:
                          >>
                          >>>Michael White wrote on 14 feb 2007 in comp.lang.javas cript:
                          >>>
                          >>>
                          >>>
                          >>>>>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.
                          >
                          They both "work"...
                          "aolahoo" and "aoyahoo" are not matched in your example.
                          I don't know why, though.
                          Mick


                          Mick

                          Comment

                          Working...