treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Getting a "Enter Valid Email Address" error

  • Hi all,
    When I hit submit on my contact form I am getting an error telling me that the email address that was entered is not valid, even though I am testing it out with valid email addresses.
    I will go ahead and post up the pattern I am using to validate:
    // If the email is set, validate and store it
    if(!empty($_POST['cf_e']) && $_POST['cf_e']!='Email (required)')
    {
    // Define a regex pattern to validate the email address
    $pattern = \"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i\";
    if (preg_match($pattern, $_POST['cf_e']))
    {
    $email = $_POST['cf_e'];
    }

    else
    {
    // If the email doesn't match the pattern, error
    $err .= \"Please enter a valid email address!<br />\";
    }
    }

    I even looked up another pattern to use and it still didn't work. Does anyone have any suggestions?
  • Well I got the error issue taken care of, but something is still wrong and I just can't figure it out.
    When I send the message, it looks like everything works fine and goes through with no errors, but when I check my email there is no message there. Anyone have any ideas what might be causing this?
  • Are you working with PHP on your local machine, or on an external server and FTPing all your files up there? If you are using your computer with something like MAMP or XAMMP set up, most likely the server won't be set up properly to send mail. I'd suggest uploading your files to your web host and testing it live there.
  • Thanks for the reply. I wish I could say that I wasn't using my external server, but yes, I am FTPing them to my hosts server.
  • Have you been able to send emails using that host in the past? I know some hosts disable it for spam reasons.

    (In case you don't know, you can check if it is disabled by looking at a file with this contents: <?php phpinfo(); ?>)
  • Well it has worked in the past with a different contact form script that I used to use. I got too much spam with that one and wanted to try a new one. So I'm pretty sure it isn't disabled, but thanks for your input! I'll take any suggestions! I'm ready to pull my hair out. It doesn't help that I'm ignorant to php! I uploaded the php to a syntax error checker and it passed it fine though.