checking email address for @ and . characters

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

    checking email address for @ and . characters

    Hi all!

    Can anyone show me how to check and email field on a form for the
    existence of these two characters.

    Kind regards

    Marc

  • Bart Van der Donck

    #2
    Re: checking email address for @ and . characters

    libsfan01 wrote:
    [color=blue]
    > Can anyone show me how to check and email field on a form for the
    > existence of these two characters.[/color]

    <html>
    <head>
    <meta http-equiv="Content-Type"
    content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    // validates the format somechars@other chars.yetotherchars
    function cM() {
    /.+@.+\..+/.test(document. mF.email.value) ?
    alert('OK') : alert('not OK');
    }
    </script>
    </head>
    <body>
    <form name="mF" action="script. php">
    <input type="text" name="email">
    <input type="button" value="Check!" onClick="cM()">
    </form>
    </body>
    </html>

    (thanks Dr John Stockton,
    http://www.merlyn.demon.co.uk/js-valid.htm#VEmA)

    --
    Bart

    Comment

    • Rik

      #3
      Re: checking email address for @ and . characters

      Bart Van der Donck wrote:[color=blue]
      > function cM() {
      > /.+@.+\..+/.test(document. mF.email.value) ?
      > alert('OK') : alert('not OK');
      > }[/color]


      Or
      /^[0-9a-z+_\.\-]+@([0-9a-z+_\.\-]+\.)+[a-z]{2,4}$/i

      Allthough I'm not sure which characters are actually allowed in an
      emailadress.

      With the above code .@...%0dBcc:spa m@example.net;m ore@example.net seems a
      totally valid emailadress..
      Then again, I don't know where the validation is for, it could be
      sufficient.

      Grtz,
      --
      Rik Wasmus


      Comment

      • Noggon

        #4
        Re: checking email address for @ and . characters


        Rik wrote:[color=blue]
        > Bart Van der Donck wrote:[color=green]
        > > function cM() {
        > > /.+@.+\..+/.test(document. mF.email.value) ?
        > > alert('OK') : alert('not OK');
        > > }[/color]
        >
        >
        > Or
        > /^[0-9a-z+_\.\-]+@([0-9a-z+_\.\-]+\.)+[a-z]{2,4}$/i[/color]

        It is possible to have a one chracter domain name now or soon will be,
        and a 5 character domain name too.

        eg: .travel

        Comment

        • Rik

          #5
          Re: checking email address for @ and . characters

          Noggon wrote:[color=blue]
          > Rik wrote:[color=green]
          >> Bart Van der Donck wrote:[color=darkred]
          >>> function cM() {
          >>> /.+@.+\..+/.test(document. mF.email.value) ?
          >>> alert('OK') : alert('not OK');
          >>> }[/color]
          >>
          >>
          >> Or
          >> /^[0-9a-z+_\.\-]+@([0-9a-z+_\.\-]+\.)+[a-z]{2,4}$/i[/color]
          >
          > It is possible to have a one chracter domain name now or soon will be,
          > and a 5 character domain name too.[/color]

          check,
          /^[0-9a-z+_\.\-]+@([0-9a-z+_\.\-]+\.)+[a-z]{1,6}$/i

          Now, who can shed some light on allowed characters in emailadresses?

          Grtz,
          --
          Rik Wasmus


          Comment

          • Bart Van der Donck

            #6
            Re: checking email address for @ and . characters

            Rik wrote:
            [color=blue]
            > [...]
            > check,
            > /^[0-9a-z+_\.\-]+@([0-9a-z+_\.\-]+\.)+[a-z]{1,6}$/i
            >
            > Now, who can shed some light on allowed characters in emailadresses?[/color]



            --
            Bart

            Comment

            • Lasse Reichstein Nielsen

              #7
              Re: checking email address for @ and . characters

              "Rik" <luiheidsgoeroe @hotmail.com> writes:
              [color=blue]
              > /^[0-9a-z+_\.\-]+@([0-9a-z+_\.\-]+\.)+[a-z]{2,4}$/i
              >
              > Allthough I'm not sure which characters are actually allowed in an
              > emailadress.[/color]

              That's reason enough *not* to be too specific. A prefectly good looking
              email like me@example.com will probably be wrong, whereas
              me+bad@[192.168.10.2] might be correct (for another IP address at least).
              [color=blue]
              > Then again, I don't know where the validation is for, it could be
              > sufficient.[/color]

              You can't validate an email adress perfectly just by looking at it
              (i.e., check that it is an email address that will receive mail
              for the correct person). The only one who knows that is the person
              writing it. It's *really* annoying to have a correct email address
              rejected by an overzealous validator, so it's better to err on the
              side of caution. For that, I prefer just checking for "@" and "."
              in that order, which will at least catch really bad errors.

              As for the specification, mail addresses were specified in RFC 822,
              and probably revised in RFC 2822. Here is someone who has tried
              interpreting it: <URL:http://www.cs.tut.fi/~jkorpela/rfc/822addr.html>

              /L
              --
              Lasse Reichstein Nielsen - lrn@hotpop.com
              DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
              'Faith without judgement merely degrades the spirit divine.'

              Comment

              • Rik

                #8
                Re: checking email address for @ and . characters

                Bart Van der Donck wrote:[color=blue]
                > Rik wrote:
                >[color=green]
                >> [...]
                >> check,
                >> /^[0-9a-z+_\.\-]+@([0-9a-z+_\.\-]+\.)+[a-z]{1,6}$/i
                >>
                >> Now, who can shed some light on allowed characters in emailadresses?[/color]
                >
                > http://en.wikipedia.org/wiki/Email_address#Limitations[/color]


                Hmmz, is someone can elaborate on the _exact_ meaning of 'any hyphen of the
                ascii character set'. Maybe it's the language difference, but if I translate
                hyphen the only one i know is '-'....

                So,
                /^[0-9a-z!#$%&'*+\-\/=?^_`{|}~]+(\.*[0-9a-z!#$%&'*+\-\/=?^_`{|}~]+)*@([0-9a-
                z\-]+\.)+[a-z]{1,6}$/i


                Then again, let's not go as far as Friedl's tester, quoted emailadresses are
                possible, a terrible, terrible thing to adjust for within limited code...

                Grtz,
                --
                Rik Wasmus


                Comment

                • Rik

                  #9
                  Re: checking email address for @ and . characters

                  Lasse Reichstein Nielsen wrote:[color=blue]
                  > "Rik" <luiheidsgoeroe @hotmail.com> writes:
                  >[color=green]
                  >> /^[0-9a-z+_\.\-]+@([0-9a-z+_\.\-]+\.)+[a-z]{2,4}$/i
                  >>
                  >> Allthough I'm not sure which characters are actually allowed in an
                  >> emailadress.[/color]
                  >
                  > That's reason enough *not* to be too specific. A prefectly good
                  > looking email like me@example.com will probably be wrong, whereas
                  > me+bad@[192.168.10.2] might be correct (for another IP address at
                  > least).
                  >[color=green]
                  >> Then again, I don't know where the validation is for, it could be
                  >> sufficient.[/color]
                  >
                  > You can't validate an email adress perfectly just by looking at it
                  > (i.e., check that it is an email address that will receive mail
                  > for the correct person). The only one who knows that is the person
                  > writing it. It's *really* annoying to have a correct email address
                  > rejected by an overzealous validator, so it's better to err on the
                  > side of caution. For that, I prefer just checking for "@" and "."
                  > in that order, which will at least catch really bad errors.[/color]

                  Well, then I'd at least do this:
                  /^[^\.@]+(\.*[^\.@]+)*@[^\.@](\.*[^\.@]+)+$/


                  Checking for the disallowes starting/ending dots is relativly simple to
                  incorporate.
                  It all depends on where you are going to use the emailadress for I suppose.

                  Grtz,
                  --
                  Rik Wasmus


                  Comment

                  • Bart Van der Donck

                    #10
                    Re: checking email address for @ and . characters

                    Rik wrote:
                    [color=blue]
                    > [...]
                    >
                    > Well, then I'd at least do this:
                    > /^[^\.@]+(\.*[^\.@]+)*@[^\.@](\.*[^\.@]+)+$/
                    >
                    > Checking for the disallowes starting/ending dots is relativly simple to
                    > incorporate.
                    > It all depends on where you are going to use the emailadress for I suppose.[/color]

                    There's much more to it. If you're planning to write a solid email
                    address validator, I think you should at least scope it down to the RFC
                    you wish to comply with. Nowadays it looks as if RFC2822 would be a
                    good choice.

                    Javascript and Perl regular expression syntaces are quite similar,
                    maybe the following could be a start:


                    (=for RFC2822)

                    --
                    Bart

                    Comment

                    • libsfan01

                      #11
                      Re: checking email address for @ and . characters

                      Sorry but i didnt get much from the solution, im looking for the JS
                      equivalent of php's function: strstr ( $haystack, $needle ) which
                      returns false if the needle is not found in the haystack.

                      is there something i can use which looks like this:

                      if (strstr( form.email.valu e, '@') == FALSE) { return false }


                      Bart Van der Donck wrote:[color=blue]
                      > Rik wrote:
                      >[color=green]
                      > > [...]
                      > >
                      > > Well, then I'd at least do this:
                      > > /^[^\.@]+(\.*[^\.@]+)*@[^\.@](\.*[^\.@]+)+$/
                      > >
                      > > Checking for the disallowes starting/ending dots is relativly simple to
                      > > incorporate.
                      > > It all depends on where you are going to use the emailadress for I suppose.[/color]
                      >
                      > There's much more to it. If you're planning to write a solid email
                      > address validator, I think you should at least scope it down to the RFC
                      > you wish to comply with. Nowadays it looks as if RFC2822 would be a
                      > good choice.
                      >
                      > Javascript and Perl regular expression syntaces are quite similar,
                      > maybe the following could be a start:
                      >
                      > http://search.cpan.org/~cwest/Email-...ail/Address.pm
                      > (=for RFC2822)
                      >
                      > --
                      > Bart[/color]

                      Comment

                      • libsfan01

                        #12
                        Re: checking email address for @ and . characters

                        am i right in thinking indexof() is the baby for this

                        libsfan01 wrote:[color=blue]
                        > Sorry but i didnt get much from the solution, im looking for the JS
                        > equivalent of php's function: strstr ( $haystack, $needle ) which
                        > returns false if the needle is not found in the haystack.
                        >
                        > is there something i can use which looks like this:
                        >
                        > if (strstr( form.email.valu e, '@') == FALSE) { return false }
                        >
                        >
                        > Bart Van der Donck wrote:[color=green]
                        > > Rik wrote:
                        > >[color=darkred]
                        > > > [...]
                        > > >
                        > > > Well, then I'd at least do this:
                        > > > /^[^\.@]+(\.*[^\.@]+)*@[^\.@](\.*[^\.@]+)+$/
                        > > >
                        > > > Checking for the disallowes starting/ending dots is relativly simple to
                        > > > incorporate.
                        > > > It all depends on where you are going to use the emailadress for I suppose.[/color]
                        > >
                        > > There's much more to it. If you're planning to write a solid email
                        > > address validator, I think you should at least scope it down to the RFC
                        > > you wish to comply with. Nowadays it looks as if RFC2822 would be a
                        > > good choice.
                        > >
                        > > Javascript and Perl regular expression syntaces are quite similar,
                        > > maybe the following could be a start:
                        > >
                        > > http://search.cpan.org/~cwest/Email-...ail/Address.pm
                        > > (=for RFC2822)
                        > >
                        > > --
                        > > Bart[/color][/color]

                        Comment

                        • Randy Webb

                          #13
                          Re: checking email address for @ and . characters

                          libsfan01 said the following on 6/24/2006 3:20 PM:[color=blue]
                          > am i right in thinking indexof() is the baby for this[/color]

                          "baby for this"? Are you writing JS or delivering babies?

                          indexof will tell you if a character is in a string but it is a long way
                          from doing what you original asked about.


                          @.yourHouse

                          indexOf('@') ==> true
                          indexOf('.') ==> true

                          @.yourMama ==> Nowhere close to an email address.
                          --
                          Randy
                          comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
                          Temporarily at: http://members.aol.com/_ht_a/hikksnotathome/cljfaq/
                          Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

                          Comment

                          • John W. Kennedy

                            #14
                            Re: checking email address for @ and . characters

                            Lasse Reichstein Nielsen wrote:[color=blue]
                            > You can't validate an email adress perfectly just by looking at it
                            > (i.e., check that it is an email address that will receive mail
                            > for the correct person). The only one who knows that is the person
                            > writing it. It's *really* annoying to have a correct email address
                            > rejected by an overzealous validator, so it's better to err on the
                            > side of caution. For that, I prefer just checking for "@" and "."
                            > in that order, which will at least catch really bad errors.[/color]

                            Even that will yield false positives in the trivial cases of mail
                            between two users on the same Unix system ("john") or two systems in the
                            same domain ("jane@payro ll" -- the ability to abbreviate
                            "payroll.wo rld-wide-wickets.com" to "payroll" in this way is the
                            historic reason that domains go right-to-left in the first place.)

                            --
                            John W. Kennedy
                            "The blind rulers of Logres
                            Nourished the land on a fallacy of rational virtue."
                            -- Charles Williams. "Taliessin through Logres: Prelude"

                            Comment

                            • Rik

                              #15
                              Re: checking email address for @ and . characters

                              Bart Van der Donck wrote:[color=blue]
                              > Javascript and Perl regular expression syntaces are quite similar,
                              > maybe the following could be a start:
                              >
                              > http://search.cpan.org/~cwest/Email-...ail/Address.pm
                              > (=for RFC2822)[/color]

                              I'm actually more at home in PERL/PHP then javascript :-).
                              I'd have to look at tha package itself, maybe tomorrow. Then again, I saw
                              Friedl's solution for complete compliace with every possible valid
                              emailadress, and was in awe. I'm instantly giving up trying to get anywhere
                              near his solution. The damned thing is, when I try to find it again, I only
                              get references to his book, and my browser history has been cleaned in the
                              mean while....

                              Then again, maybe his book is a good purchase, really mastering regular
                              expressions is usually a time-saver in the first place, and rewriting them
                              when the reuirements change much simpler then huge quantaties of custom
                              code.

                              Grtz,
                              --
                              Rik Wasmus


                              Comment

                              Working...