I'm studying a teach yourself php book and have trouble understanding the following regular expression which should check for a valid form input of an email address:

^[0-9a-zA-Z_\.-]{1,}@([0-9a-zA-Z_\-]{1,}\.)+[0-9a-zA-Z_\-]{2,}$

First of all, the book says that the username and (sub)domain part should also include an "_", "-"and "+" character. I think the "+" character should read a "." character?

If so... in:

[0-9a-zA-Z_\.-]

an escape () character is used before the "." character. I guess this is done to take the "." character literally. But I've also learned so far that the escape character doesn't have to be used if special php symbols, such as the "." character, are used between brackets. So shouldn't it be this?:

[0-9a-zA-Z_.-]

And for the top level domain part:

[0-9a-zA-Z_\-]{2,}

Shouldn't it be just this?:

[a-zA-Z]{2,}

because in top level domains no numbers and characters lide "_" "\" and "-" are used? (again, an escape character is being used between brackets where it's not necessary?)

Am I right in thinking (as a newbie to php and regexp) that the complete regexp should be something like this?:

^[0-9a-zA-Z_.-]{1,}@([0-9a-zA-Z_-]{1,}\.)+[a-zA-Z]{2,6}$

    [A-Z0-9.-]+@[A-Z0-9.-]{2,}.[A-Z]{2,4}$

    this is the one I use - it seems to work ok.

      This is the best email regexp function I've found so far:

      # email address validation function
      # kudos to http://iamcal.com/publish/articles/php/parsing_email/
      function is_valid_email_address($email) {
        $qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
        $dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
        $atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c'.
        '\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
        $quoted_pair = '\\x5c\\x00-\\x7f';
        $domain_literal = "\\x5b($dtext|$quoted_pair)*\\x5d";
        $quoted_string = "\\x22($qtext|$quoted_pair)*\\x22";
        $domain_ref = $atom;
        $sub_domain = "($domain_ref|$domain_literal)";
        $word = "($atom|$quoted_string)";
        $domain = "$sub_domain(\\x2e$sub_domain)*";
        $local_part = "$word(\\x2e$word)*";
        $addr_spec = "$local_part\\x40$domain";
        return preg_match("!^$addr_spec$!", $email) ? 1 : 0;
      }
      

      See http://iamcal.com/publish/articles/php/parsing_email/ for more info about it.

        5 months later

        I just tried that function by calling it with

        			
        if(is_valid_email_address($email2check)!=true){
        	$dataError = 1;
        }

        the email I tried is BILL@BILL.COM - which shouldn't be allowed through but was

        is my function call correct ??

        thanks

          VisionsOfCody wrote:

          I just tried that function by calling it with

          			
          if(is_valid_email_address($email2check)!=true){
          	$dataError = 1;
          }

          the email I tried is BILL@BILL.COM - which shouldn't be allowed through but was

          is my function call correct ??

          thanks

          I just ran this, and it output the expected value of "0":

          function is_valid_email_address($email) {
            $qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
            $dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
            $atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c'.
            '\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
            $quoted_pair = '\\x5c\\x00-\\x7f';
            $domain_literal = "\\x5b($dtext|$quoted_pair)*\\x5d";
            $quoted_string = "\\x22($qtext|$quoted_pair)*\\x22";
            $domain_ref = $atom;
            $sub_domain = "($domain_ref|$domain_literal)";
            $word = "($atom|$quoted_string)";
            $domain = "$sub_domain(\\x2e$sub_domain)*";
            $local_part = "$word(\\x2e$word)*";
            $addr_spec = "$local_part\\x40$domain";
            return preg_match("!^$addr_spec$!", $email) ? 1 : 0;
          }
          $email2check = 'BILL@BILL.COM'; 
          if(is_valid_email_address($email2check)!=true){
              $dataError = 1;
          }
          else
          {
              $dataError = 0;
          }
          echo $dataError;
          

          My guess is something else in your code's logic might be the problem.

            yes you're right

            I hadn't put in the

            else
            {
                $dataError = 0;
            } 

            the code wasn't letting it through - it just wasn't refusing it

            this seems like a great email checker

            thanks

              Write a Reply...