Help with email address validation using eregi()

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

    Help with email address validation using eregi()

    Hi

    The following script was taken from John Coggeshall's (PHP consultant) in his
    article on Zends site at http://www.zend.com/zend/spotlight/ev12apr.php

    // Get the email address to validate
    $email = $_POST['email']
    // Use John Coggeshalls script to validate the email address
    if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-
    ]+)*(\.[a-z]{2,3})$", $email) {
    echo "The e-mail was not valid";
    }
    else {
    echo "The e-mail was valid";
    }

    In the first instance the script does not work if I copy and paste it into my
    php file. I get a parse error message.
    In the second instance, there are many postings relating to the scripts
    effectiveness in that it will still allow email addresses that are invalid.
    So I have 3 questions.
    1) Why do I get the parse error message?
    2) Is there a definitive email validation script?
    3) Where can I find regular expressions or other form validation scripts to
    validate things such as telephone numbers, postcodes, names that don't contain
    numeric characters etc etc

    Kind regards
    Dynamo

  • John Dunlop

    #2
    Re: Help with email address validation using eregi()

    Dynamo wrote:
    [color=blue]
    > The following script was taken from John Coggeshall's (PHP consultant) in his
    > article on Zends site at http://www.zend.com/zend/spotlight/ev12apr.php
    >
    > // Get the email address to validate
    > $email = $_POST['email'][/color]

    Missing semicolon at the end.
    [color=blue]
    > eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-
    > ]+)*(\.[a-z]{2,3})$", $email)[/color]

    Too strict and too liberal at the same time.

    [ ... ]
    [color=blue]
    > So I have 3 questions.
    > 1) Why do I get the parse error message?[/color]

    Ask PHP to tell you, not us.
    [color=blue]
    > 2) Is there a definitive email validation script?[/color]

    http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html is
    often put forward here, but it requires Perl to fully
    validate an address because the pattern itself does not
    handle comments.
    [color=blue]
    > 3) Where can I find regular expressions or other form validation scripts to
    > validate things such as telephone numbers,[/color]

    DIY:

    Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!

    [color=blue]
    > postcodes,[/color]

    UK:

    `([A-Z]{1,2})(\d[A-Z\d]?)\040(\d)([ABD-HJLNP-UW-Z]{2})`
    [color=blue]
    > names that don't contain numeric characters[/color]

    Don't understand.
    [color=blue]
    > etc etc[/color]

    Cannot compute.

    --
    Jock

    Comment

    • Dynamo

      #3
      Re: Help with email address validation using eregi()

      In article <MPG.1c19149b1a fc0d109897d6@Ne ws.Individual.N ET>, John Dunlop says...[color=blue]
      >[color=green]
      >> // Get the email address to validate
      >> $email = $_POST['email'][/color]
      >
      >Missing semicolon at the end.
      >[color=green]
      >> eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-
      >> ]+)*(\.[a-z]{2,3})$", $email)[/color]
      >
      >Too strict and too liberal at the same time.
      >
      >[ ... ]
      >[color=green]
      >> So I have 3 questions.
      >> 1) Why do I get the parse error message?[/color]
      >
      >Ask PHP to tell you, not us.[/color]

      Granted the semi-colon is missing in my post but it is present in my actual
      script.

      PHP simply sais Parse error. Unexpected '}' but I cannot see where.

      if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-
      ]+)*(\.[a-z]{2,3})$", $email) {
      echo "The e-mail was not valid";
      }
      else {
      echo "The e-mail was valid";
      }

      This following script is taken from a posting by an 11 year old to validate
      passwords.

      if (!ereg('^[[:alnum:]]+$', $pass)) {
      $err14 = "Password contains illegal characters</br>";
      $errors = 1;
      }

      AND IT WORKS! Now if an 11 year old can do that and yet a php consultant posts a
      script that doesn't work then where does that leave a 50 year old newbie like
      me! DAZED and CONFUSED.

      Regards
      Dynamo

      Comment

      • Alan Little

        #4
        Re: Help with email address validation using eregi()

        Carved in mystic runes upon the very living rock, the last words of
        Dynamo of comp.lang.php make plain:
        [color=blue]
        > The following script was taken from John Coggeshall's (PHP consultant)
        > in his article on Zends site at
        > http://www.zend.com/zend/spotlight/ev12apr.php
        >
        > // Get the email address to validate
        > $email = $_POST['email']
        > // Use John Coggeshalls script to validate the email address
        > if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-
        >]+)*(\.[a-z]{2,3})$", $email) {[/color]
        [color=blue]
        > 1) Why do I get the parse error message?[/color]

        You're missing a closing parenthesis at the end of your condition. But
        this regex is not valid anyway. I don't know about it passing invalid
        address, but it will not pass many valid ones. The fact is, virtually
        anything is allowed to the left of the @ sign, and all regexes I've seen
        for email validation fail to allow for this. Further, something like
        "fpoiuapoiausd@ ufidos.yy.zzz" would pass, but is obviously not a valid
        address. The way to validate an email address is to break it down into
        its parts and analyze the parts, for example:
        [color=blue]
        > 2) Is there a definitive email validation script?[/color]

        There's a pretty good email validation function in HoloLib:

        ftp://ftp.holotech.net/hololib.zip
        [color=blue]
        > 3) Where can I find regular expressions or other form validation
        > scripts to validate things such as telephone numbers, postcodes, names
        > that don't contain numeric characters etc etc[/color]



        --
        Alan Little
        Phorm PHP Form Processor

        Comment

        • Daniel Tryba

          #5
          Re: Help with email address validation using eregi()

          Dynamo <Dynamo_member@ newsguy.com> wrote:[color=blue][color=green][color=darkred]
          >>> 1) Why do I get the parse error message?[/color]
          >>
          >>Ask PHP to tell you, not us.[/color]
          >
          > Granted the semi-colon is missing in my post but it is present in my actual
          > script.[/color]

          Then post the real code!
          [color=blue]
          > PHP simply sais Parse error. Unexpected '}' but I cannot see where.[/color]

          Is this the _real_ error? Are you sure it isn't: Unexpected '{' ?
          [color=blue]
          > if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-
          > ]+)*(\.[a-z]{2,3})$", $email) {[/color]

          There is a ')' missing here ^
          [color=blue]
          > This following script is taken from a posting by an 11 year old to validate
          > passwords.
          >
          > if (!ereg('^[[:alnum:]]+$', $pass)) {
          > $err14 = "Password contains illegal characters</br>";
          > $errors = 1;
          > }[/color]
          [color=blue]
          > AND IT WORKS![/color]

          No it doesn't... all it does is force weak passwords
          [color=blue]
          > Now if an 11 year old can do that and yet a php consultant posts a
          > script that doesn't work then where does that leave a 50 year old
          > newbie like me! DAZED and CONFUSED.[/color]

          What are you trying to accomplish? IMHO email "validation " in PHP is a
          total waste of time and resources. The regexp you found "validates"
          the obvious invalid '-@-.xx' as a valid emailaddress, and the very possibly
          valid 'first@last.nam e' as invalid.

          If you want to make sure the email is valid, the only way is to actually
          send a challenge to the emailaddress and wait for the response.

          Comment

          • Dynamo

            #6
            Re: Help with email address validation using eregi()

            In article <Xns95B35F1D08F 45alanphormcom@ 216.196.97.132> , Alan Little says...[color=blue][color=green]
            >> if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-
            >>]+)*(\.[a-z]{2,3})$", $email) {[/color]
            >[color=green]
            >> 1) Why do I get the parse error message?[/color]
            >
            >You're missing a closing parenthesis at the end of your condition.[/color]

            In my original post the closing parenthisis was there in the else part of the
            statement or am I going mad?

            if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-
            ]+)*(\.[a-z]{2,3})$", $email) {
            echo "The e-mail was not valid";
            }
            else {
            echo "The e-mail was valid";
            } // closing parenthisis is here

            But thanks for the other info.
            Regards
            Dynamo


            Comment

            • Alan Little

              #7
              Re: Help with email address validation using eregi()

              Carved in mystic runes upon the very living rock, the last words of
              Dynamo of comp.lang.php make plain:
              [color=blue]
              > In article <Xns95B35F1D08F 45alanphormcom@ 216.196.97.132> , Alan Little
              > says...[color=green][color=darkred]
              >>> if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-
              >>>]+)*(\.[a-z]{2,3})$", $email) {[/color]
              >>[color=darkred]
              >>> 1) Why do I get the parse error message?[/color]
              >>
              >>You're missing a closing parenthesis at the end of your condition.[/color][/color]
              [color=blue]
              > In my original post the closing parenthisis was there in the else part
              > of the statement or am I going mad?[/color]

              You're thinking of the curly braces for the code block. I'm talking about
              the parenthesis around the condition. You have:
              [color=blue]
              > if(!eregi("[the pattern]", $email) {[/color]
              ^
              parenthesis missing


              --
              Alan Little
              Phorm PHP Form Processor

              Comment

              • Alan Little

                #8
                Re: Help with email address validation using eregi()

                Carved in mystic runes upon the very living rock, the last words of
                Daniel Tryba of comp.lang.php make plain:
                [color=blue]
                > What are you trying to accomplish? IMHO email "validation " in PHP is a
                > total waste of time and resources.
                >
                > If you want to make sure the email is valid, the only way is to
                > actually send a challenge to the emailaddress and wait for the
                > response.[/color]

                Ultimately, yes, that's the only way to know if an email address is valid:
                send something and see if it bounces. But you can do some pre-checking that
                is worthwhile for catching things like typos and so forth.

                --
                Alan Little
                Phorm PHP Form Processor

                Comment

                • keith@keithslater.com

                  #9
                  Re: Help with email address validation using eregi()

                  I can answer one of your questions...

                  This line:
                  $email = $_POST['email']

                  needs to be this:
                  $email = $_POST['email'];

                  I would also take a look at this site:

                  An introduction to and overview of the PHP language, the basics of its syntax and usage, its beginner friendliness and power for large web applications.

                  You may just want to make your own regular expressions.

                  Comment

                  • keith@keithslater.com

                    #10
                    Re: Help with email address validation using eregi()

                    I can answer one of your questions...

                    This line:
                    $email = $_POST['email']

                    needs to be this:
                    $email = $_POST['email'];

                    I would also take a look at this site:

                    An introduction to and overview of the PHP language, the basics of its syntax and usage, its beginner friendliness and power for large web applications.

                    You may just want to make your own regular expressions.

                    Comment

                    • Daniel Tryba

                      #11
                      Re: Help with email address validation using eregi()

                      Alan Little <alan@n-o-s-p-a-m-phorm.com> wrote:[color=blue][color=green]
                      >> If you want to make sure the email is valid, the only way is to
                      >> actually send a challenge to the emailaddress and wait for the
                      >> response.[/color]
                      >
                      > Ultimately, yes, that's the only way to know if an email address is valid:
                      > send something and see if it bounces.[/color]

                      No, that's not the same, I can enter any valid address that isn't mine
                      and have it not bounce. Challenge/response as in: (double)opt-in
                      procedure.
                      [color=blue]
                      > But you can do some pre-checking that is worthwhile for catching
                      > things like typos and so forth.[/color]

                      If it's that important ask the question twice (like is standard with
                      passwords) and asking the user for confirmation...

                      Comment

                      • powdahound

                        #12
                        Re: Help with email address validation using eregi()

                        What is the parse error that you get?

                        Comment

                        • Peter

                          #13
                          Re: Help with email address validation using eregi()

                          Put the following javascript in the onsubmit() method of the form i.e
                          <form name=form1 onsubmit='retur n isReady(this)'>

                          function isReady(form) // assuming the form variable is called $email
                          {
                          if(form.email.v alue.indexOf("@ ")=="-1" ||
                          form.email.valu e.indexOf(".")= ="-1" )
                          {
                          alert("Please enter a valid email address");
                          form.cust_email .select();
                          form.cust_email .focus();
                          return false;
                          }
                          return true;
                          }


                          Dynamo wrote:[color=blue]
                          > Hi
                          >
                          > The following script was taken from John Coggeshall's (PHP consultant) in his
                          > article on Zends site at http://www.zend.com/zend/spotlight/ev12apr.php
                          >
                          > // Get the email address to validate
                          > $email = $_POST['email']
                          > // Use John Coggeshalls script to validate the email address
                          > if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-
                          > ]+)*(\.[a-z]{2,3})$", $email) {
                          > echo "The e-mail was not valid";
                          > }
                          > else {
                          > echo "The e-mail was valid";
                          > }
                          >
                          > In the first instance the script does not work if I copy and paste it into my
                          > php file. I get a parse error message.
                          > In the second instance, there are many postings relating to the scripts
                          > effectiveness in that it will still allow email addresses that are invalid.
                          > So I have 3 questions.
                          > 1) Why do I get the parse error message?
                          > 2) Is there a definitive email validation script?
                          > 3) Where can I find regular expressions or other form validation scripts to
                          > validate things such as telephone numbers, postcodes, names that don't contain
                          > numeric characters etc etc
                          >
                          > Kind regards
                          > Dynamo
                          >[/color]

                          Comment

                          • Dynamo

                            #14
                            Re: Help with email address validation using eregi()

                            In article <41af2f7d$0$789 86$e4fe514c@dre ader3.news.xs4a ll.nl>, Daniel Tryba
                            says...[color=blue]
                            >
                            >Dynamo <Dynamo_member@ newsguy.com> wrote:[color=green][color=darkred]
                            >>>> 1) Why do I get the parse error message?
                            >>>
                            >>>Ask PHP to tell you, not us.[/color]
                            >>
                            >> Granted the semi-colon is missing in my post but it is present in my actual
                            >> script.[/color]
                            >
                            >Then post the real code![/color]
                            [snip]
                            Humblest apologies
                            [color=blue][color=green]
                            >> This following script is taken from a posting by an 11 year old to validate
                            >> passwords.
                            >>
                            >> if (!ereg('^[[:alnum:]]+$', $pass)) {
                            >> $err14 = "Password contains illegal characters</br>";
                            >> $errors = 1;
                            >> }[/color]
                            >[color=green]
                            >> AND IT WORKS![/color]
                            >
                            >No it doesn't... all it does is force weak passwords
                            >[/color]
                            [snip]

                            As a newbie, and having tried loads of supposed ready made validation scripts
                            that didn't work, I simply found it quite ironic that an 11 year old posted a
                            script that actually worked. When I say "worked" I mean it didn't cause any
                            parse error messages or any warnings as did other scripts from supposed php
                            consultants. Wether it is effective or not in that it may force weak passwords
                            is another issue far beyond my comprehension. It is not my intention to upset
                            anybody in this newsgroup and I appreciate your comments and will try adding the
                            extra parenthisis.

                            Kind Regards
                            Dynamo

                            Comment

                            • Dynamo

                              #15
                              Re: Help with email address validation using eregi()

                              In article <1102010345.578 850.153390@z14g 2000cwz.googleg roups.com>, powdahound
                              says...[color=blue]
                              >
                              >What is the parse error that you get?
                              >[/color]
                              Thanks but I think its sorted

                              Comment

                              Working...