invalid email? please check over basic script

Discussion in 'PHP' started by mezner, Jun 12, 2010.

  1. #1
    I don't know why, but whenever I enter an email it keeps coming up as invalid! Can someone help me figure out why it's coming out as invalid? also, is there a way to debug this, so I will know how in the future? Thanks

    <?php
    
        // Function used to check e-mail syntax
        function validateEmail($email)
    
        {
           // Create the e-mail validation regular expression
           $regexp = "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)
                      (\.[a-z0-9-]+)*(\.[a-z]{2,6})$";
    
           // Validate the syntax
           if (eregi($regexp, $email)) return 1;
               else return 0;
        }
    
        // Has the form been submitted?
        if (isset($_POST['submit']))
        {
    
            $name = htmlentities($_POST['name']);
            $email = htmlentities($_POST['email']);
    
            printf("Hi %s<br />", $name);
    
            if (validateEmail($email))
                printf("The address %s is valid!", $email);
            else
                printf("The address <strong>%s</strong> is invalid!", $email);
        }
    ?>
    
    <form action="subscribe.php" method="post">
       <p>
          Name:<br />
          <input type="text" id="name" name="name" size="20" maxlength="40" />
       </p>
    
       <p>
          E-mail Address:<br />
          <input type="text" id="email" name="email" size="20" maxlength="40" />
       </p>
    
       <input type="submit" id="submit" name = "submit" value="Go!" />
    </form>
    
    PHP:

     
    mezner, Jun 12, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    function validateEmail($email)
    
        {
           // Create the e-mail validation regular expression
           $regexp = "~^([_a-z0-9\-]+)(\.[_a-z0-9\-]+)*@([a-z0-9\-]+)
                      (\.[a-z0-9\-]+)*(\.[a-z]{2,6})$~";
    
           // Validate the syntax
           if (preg_match($regexp, $email)) return true;
               else return false;
        }
    PHP:
     
    danx10, Jun 12, 2010 IP
  3. mezner

    mezner Peon

    Messages:
    289
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Still coming up as an invalid email.
     
    mezner, Jun 12, 2010 IP
  4. realturk

    realturk Active Member

    Messages:
    38
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #4
    
    <?php
     $email = "xx@xx.com";
     if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {  
     echo "Valid email address."; 
    } else {   
    echo "Invalid email address."; 
    }  
    ?> 
    
    PHP:
    regards...
     
    realturk, Jun 12, 2010 IP
  5. realturk

    realturk Active Member

    Messages:
    38
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #5
    
    preg_match('/^[^@]+@[a-zA-Z0-9._-]+\.[a-zA-Z]+$/', $email)
    
    PHP:
    preg_match pattern...
     
    realturk, Jun 12, 2010 IP
  6. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #6
    Yeah you can use Preg Pattern for this!
     
    roopajyothi, Jun 12, 2010 IP
  7. Scripts man

    Scripts man Guest

    Messages:
    51
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
  8. strgraphics

    strgraphics Active Member

    Messages:
    710
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #8
    preg_match is the best solution.., preg type is the good concept in php.
     
    strgraphics, Jun 15, 2010 IP
  9. johny321

    johny321 Member

    Messages:
    293
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #9
    yes Preg_match is the only easy way to solve this problem
     
    johny321, Jun 16, 2010 IP
  10. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #10
    Just wanted to say, that the majority of email address would be in the format of if the email address your trying to validate contains any symbols or extra .dots you must allow your preg_match pattern to allow for this. So although this thread is a few days old, if you do reply, please post an example of the email address your trying to validate.
     
    MyVodaFone, Jun 16, 2010 IP