Checking for a valid email address

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

    Checking for a valid email address

    In a web based form I am able to make sure that there is text in an input field but I want to force the user into inputting a valid email
    address, one that has @ in the address

    How can I modify this JavaScript below to enable this ?

    if (document.form1 .EMAIL.value == ""){
    alert("Please complete the E-Mail: field")
    document.form1. EMAIL.focus()
    validFlag = false
    return validFlag
    }

    Kindest Regards - Philip Amey

  • kaeli

    #2
    Re: Checking for a valid email address

    In article <4003E11D.FF6E3 B53@btinternet. com>, philamey@btinte rnet.com
    enlightened us with...[color=blue]
    > In a web based form I am able to make sure that there is text in an input field but I want to force the user into inputting a valid email
    > address, one that has @ in the address
    >
    > How can I modify this JavaScript below to enable this ?
    >
    > if (document.form1 .EMAIL.value == ""){
    > alert("Please complete the E-Mail: field")
    > document.form1. EMAIL.focus()
    > validFlag = false
    > return validFlag
    > }
    >
    > Kindest Regards - Philip Amey
    >
    >[/color]

    (these are my functions from my validation file, so you can put this all
    in one if you like, I just copied/pasted them here)

    function isBlank(strObje ct)
    {
    /* Returns true if the field is blank, false if not.
    You must pass in an input (text) object (not the value) */
    var re = /\S+/;

    if (!strObject) return true;
    if (re.test(strObj ect.value)) return false;
    else return true;
    }

    function isEmail(strObje ct)
    {
    // returns true if: something@somet hing.com
    var re = /^.+@.*\.com$/i;
    if (!strObject || isBlank(strObje ct)) return false;
    if (!re.test(strOb ject.value)) return false;
    else return true;
    }

    if (! isEmail(documen t.form1.EMAIL)) {

    --
    --
    ~kaeli~
    Once you've seen one shopping center, you've seen a mall.



    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: Checking for a valid email address

      kaeli <tiny_one@NOSPA M.comcast.net> writes:
      [color=blue]
      > function isEmail(strObje ct)
      > {
      > // returns true if: something@somet hing.com
      > var re = /^.+@.*\.com$/i;[/color]

      As a non-.com-user, I would recommend just
      var re = /.+@.+\..+/;
      It accepts anything with a @ and a . in that order and with something
      around it. Most attempts at being more precise will usually rule out
      some perfectly good e-mail address.

      /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

      • Christopher Jeris

        #4
        Re: Checking for a valid email address

        Lasse Reichstein Nielsen <lrn@hotpop.com > writes:[color=blue]
        > As a non-.com-user, I would recommend just
        > var re = /.+@.+\..+/;
        > It accepts anything with a @ and a . in that order and with something
        > around it. Most attempts at being more precise will usually rule out
        > some perfectly good e-mail address.[/color]

        Danny Goodman recommends

        /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/

        (JavaScript & DHTML Cookbook section 8.2). Is there a known problem
        with this expression?

        (briefly, word (.word)* @ (word.)+ gTLD, where 'word' := [\w-]+ and
        'gTLD' := [a-zA-Z]{2,7}.)

        I suppose it might be better to write [[:alnum:]_-] for [\w-]
        and [[:alpha:]] for [a-zA-Z] ?

        --
        Chris Jeris cjeris@oinvzer. net Apply (1 6 2 4)(3 7) to domain to reply.


        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: Checking for a valid email address

          Christopher Jeris <cjeris@oinvzer .net> writes:
          [color=blue]
          > Danny Goodman recommends
          >
          > /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/
          > (JavaScript & DHTML Cookbook section 8.2). Is there a known problem
          > with this expression?[/color]

          It doesn't allow
          me+test@example .com
          or
          me@[127.0.0.1]
          which are both valid e-mail addresses.

          A validator of e-mail addresses should first and foremost accept all
          valid addresses, otherwise it will interfere with proper use. So, if
          it errs, it should err on the side of safety and accept unless there
          is a reason to reject. It should at least accept all valid adresses
          according to RFC 2822 section 3.4 (and that's a lot).
          ---quote---
          An addr-spec is a specific Internet identifier that contains a
          locally interpreted string followed by the at-sign character ("@",
          ASCII value 64) followed by an Internet domain.
          -----------

          Maybe I shouldn't test for the period, just the presence of @.
          [color=blue]
          > I suppose it might be better to write [[:alnum:]_-] for [\w-]
          > and [[:alpha:]] for [a-zA-Z] ?[/color]

          Those are Perl's long class names. It won't work in Javascript.

          (I wish there was an escape for "\w except \d" :)
          /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

          • Christopher Jeris

            #6
            Re: Checking for a valid email address

            Lasse Reichstein Nielsen <lrn@hotpop.com > writes:[color=blue]
            > It should at least accept all valid adresses
            > according to RFC 2822 section 3.4 (and that's a lot).
            > ---quote---
            > An addr-spec is a specific Internet identifier that contains a
            > locally interpreted string followed by the at-sign character ("@",
            > ASCII value 64) followed by an Internet domain.
            > -----------[/color]

            Well, the text following your quote indicates that there are a _few_
            restrictions (for instance, I don't believe an unquoted left bracket
            is legal in an 'addr-spec'), but you're absolutely correct, Goodman's
            expression is too restrictive to match all addresses. Thank you.
            [color=blue][color=green]
            > > I suppose it might be better to write [[:alnum:]_-] for [\w-]
            > > and [[:alpha:]] for [a-zA-Z] ?[/color]
            > Those are Perl's long class names. It won't work in Javascript.[/color]

            Doh! Stupid Chris reads the ColdFusion manual, sees that CF
            implemented (some of the) long class names from POSIX, and infers
            without any justification whatsoever that they work in JavaScript
            too. Sorry! (It made sense at the time, honest.)

            --
            Chris Jeris cjeris@oinvzer. net Apply (1 6 2 4)(3 7) to domain to reply.

            Comment

            • kaeli

              #7
              Re: Checking for a valid email address

              In article <isjfwpyb.fsf@h otpop.com>, lrn@hotpop.com enlightened us
              with...[color=blue]
              > kaeli <tiny_one@NOSPA M.comcast.net> writes:
              >[color=green]
              > > function isEmail(strObje ct)
              > > {
              > > // returns true if: something@somet hing.com
              > > var re = /^.+@.*\.com$/i;[/color]
              >
              > As a non-.com-user, I would recommend just[/color]

              Sorry about that.
              I forget about all the variations of the WWW sometimes (you guys keep me
              well-rounded LOL).
              This was taken from an intranet snippet where the only valid e-mail was
              something@mycom pany.com.

              I like the other one you posted for WWW use.
              var re = /.+@.+\..+/;

              Thanks!

              --
              --
              ~kaeli~
              All I ask is the chance to prove that money cannot make me
              happy.



              Comment

              • Dr John Stockton

                #8
                Re: Checking for a valid email address

                JRS: In article <isjfwpyb.fsf@h otpop.com>, seen in
                news:comp.lang. javascript, Lasse Reichstein Nielsen <lrn@hotpop.com >
                posted at Tue, 13 Jan 2004 21:31:24 :-
                [color=blue]
                >As a non-.com-user, I would recommend just
                > var re = /.+@.+\..+/;
                >It accepts anything with a @ and a . in that order and with something
                >around it. Most attempts at being more precise will usually rule out
                >some perfectly good e-mail address.[/color]


                There is the question of purpose.

                If the wish is to accept anything that includes what might be an E-mail
                address, one may need just that. It does, however, accept itself, which
                seems generous.

                I doubt whether anyone would intentionally provide a "normal" address
                with anything but letters (plural) in the final field, so such a test
                for that field being alpha 2+ should be safe enough, and would catch a
                few gross blunders.


                It is not the test used by my mailer Turnpike, though.

                Turnpike will accept a mere lrn@hotpop.com
                and <lrn@hotpop.com >
                but rightly also a full Lasse Reichstein Nielsen <lrn@hotpop.com >
                It rightly rejects Lasse R. Nielsen <lrn@hotpop.com >
                and accepts "Lasse R. Nielsen" <lrn@hotpop.com >
                It rightly accepts also the possibly-deprecated form
                lrn@hotpop.com (Denmark)
                It rejects, in those, unmatched punctuation.


                Where a form requires that an E-mail address be provided, the designer
                should consider whether a fuller form should be allowed; and, if so,
                that fuller form should be validated to Internet standards.

                If collecting name & address * date of birth & e-mail address, the extra
                may not be needed. But if the E-address given is to be used, then one
                should IMHO be allowed to give it in a full form.

                --
                © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
                <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
                <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

                Comment

                • Alan P

                  #9
                  Re: Checking for a valid email address

                  Just a minor note:

                  From a server-side point of view, you can write a Perl program that pings
                  the domain to check it exists; stopping people from writing a@a

                  "Christophe r Jeris" <cjeris@oinvzer .net> wrote in message
                  news:ximvfnf7cv c.fsf@hsph.harv ard.edu...[color=blue]
                  > Lasse Reichstein Nielsen <lrn@hotpop.com > writes:[color=green]
                  > > It should at least accept all valid adresses
                  > > according to RFC 2822 section 3.4 (and that's a lot).
                  > > ---quote---
                  > > An addr-spec is a specific Internet identifier that contains a
                  > > locally interpreted string followed by the at-sign character ("@",
                  > > ASCII value 64) followed by an Internet domain.
                  > > -----------[/color]
                  >
                  > Well, the text following your quote indicates that there are a _few_
                  > restrictions (for instance, I don't believe an unquoted left bracket
                  > is legal in an 'addr-spec'), but you're absolutely correct, Goodman's
                  > expression is too restrictive to match all addresses. Thank you.
                  >[color=green][color=darkred]
                  > > > I suppose it might be better to write [[:alnum:]_-] for [\w-]
                  > > > and [[:alpha:]] for [a-zA-Z] ?[/color]
                  > > Those are Perl's long class names. It won't work in Javascript.[/color]
                  >
                  > Doh! Stupid Chris reads the ColdFusion manual, sees that CF
                  > implemented (some of the) long class names from POSIX, and infers
                  > without any justification whatsoever that they work in JavaScript
                  > too. Sorry! (It made sense at the time, honest.)
                  >
                  > --
                  > Chris Jeris cjeris@oinvzer. net Apply (1 6 2 4)(3 7) to domain to reply.[/color]


                  Comment

                  • Randy Webb

                    #10
                    Re: Checking for a valid email address

                    Alan P wrote:[color=blue]
                    > Just a minor note:
                    >
                    > From a server-side point of view, you can write a Perl program that pings
                    > the domain to check it exists; stopping people from writing a@a
                    >[/color]

                    JustHikk@aol.co m is a very valid email address. But unless you are
                    sending it mail from HikksNotAtHome@ aol.com, then it won't get delivered.

                    While you have a valid point about a@a, the idea of trying to ensure an
                    email address is "valid" is two-fold:

                    1) Is it a valid format
                    2) Does the email address actually exist?

                    format is an overly discussed subject and existence has been debated
                    before. Neither of which is very easy to accomplish.

                    The only way I know of to ensure and email address actually exists is to
                    try to email it and see if it goes through, which still doesn't confirm
                    its ability to recieve email.

                    --
                    Randy

                    Comment

                    • Alan P

                      #11
                      Re: Checking for a valid email address

                      Hmmmm thats a good idea

                      Like on many web-forums, where you have to activate the e-mail address
                      before being granted access

                      Don't think that solution is applicable here though

                      "Randy Webb" <hikksnotathome @aol.com> wrote in message
                      news:kuudnWSIK-iZFpvdRVn-hQ@comcast.com. ..[color=blue]
                      > Alan P wrote:[color=green]
                      > > Just a minor note:
                      > >
                      > > From a server-side point of view, you can write a Perl program that[/color][/color]
                      pings[color=blue][color=green]
                      > > the domain to check it exists; stopping people from writing a@a
                      > >[/color]
                      >
                      > JustHikk@aol.co m is a very valid email address. But unless you are
                      > sending it mail from HikksNotAtHome@ aol.com, then it won't get delivered.
                      >
                      > While you have a valid point about a@a, the idea of trying to ensure an
                      > email address is "valid" is two-fold:
                      >
                      > 1) Is it a valid format
                      > 2) Does the email address actually exist?
                      >
                      > format is an overly discussed subject and existence has been debated
                      > before. Neither of which is very easy to accomplish.
                      >
                      > The only way I know of to ensure and email address actually exists is to
                      > try to email it and see if it goes through, which still doesn't confirm
                      > its ability to recieve email.
                      >
                      > --
                      > Randy
                      >[/color]


                      Comment

                      • Dr John Stockton

                        #12
                        Re: Checking for a valid email address

                        JRS: In article <kuudnWSIK-iZFpvdRVn-hQ@comcast.com> , seen in
                        news:comp.lang. javascript, Randy Webb <hikksnotathome @aol.com> posted at
                        Thu, 15 Jan 2004 07:42:51 :-[color=blue]
                        >Alan P wrote:[color=green]
                        >> Just a minor note:
                        >>
                        >> From a server-side point of view, you can write a Perl program that pings
                        >> the domain to check it exists; stopping people from writing a@a[/color][/color]

                        In that sense, my merlyn.dcu exists only about 2% of the time. However,
                        mail can be sent to it at any time, and, if the conditions are
                        propitious, it will be fully received.

                        OTOH, my www.merlyn.dcu exists permanently, might return a ping, and
                        never receives mail.

                        [color=blue]
                        >While you have a valid point about a@a, the idea of trying to ensure an
                        >email address is "valid" is two-fold:
                        >
                        >1) Is it a valid format
                        >2) Does the email address actually exist?
                        >
                        >format is an overly discussed subject and existence has been debated
                        >before. Neither of which is very easy to accomplish.[/color]

                        The first is certainly possible; just read all the RFCs and implement
                        correct checks. The second is certainly impossible in at least some
                        cases at some times.
                        [color=blue]
                        >The only way I know of to ensure and email address actually exists is to
                        >try to email it and see if it goes through, which still doesn't confirm
                        >its ability to recieve email.[/color]

                        The only ways to know whether an address receives mail are to mail it
                        and get a human reply, and to be at the receiving end.

                        At this moment, two addresses here receive ... but now they do not, and
                        two others do -- and the machine was not connected to the Net during
                        this sentence.

                        --
                        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                        <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
                        <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
                        <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

                        Comment

                        Working...