Skip to content
Advertisement

E-Mail Validation Error in PHP All-in-One Form

I was doing an exercise from a book and copied exactly what was in there (to my knowledge) yet I still can’t get it to process a valid E-Mail. I’ve tried different regular expressions but have had no luck. I’m pretty sure I missed something stupid (and sound pretty stupid asking for this but I am new to PHP). Can anyone take a look and see if you can see what I’m missing (or doing wrong)?

    <!DOCTYPE html>

    <html lang="en">
       <head>
       <title>Contact Me</title>
       </head>
       <header>
       </header>
       <body>
       <?php
    function validateInput($data, $fieldName) {
    global $errorCount;
    if (empty($data)) {
        echo ""$fieldName" is a required field.<br />n";
        ++$errorCount;
        $retval = "";
    } else {
        $retval = trim($data);
        $retval = stripslashes($retval);
        }
    return($retval);
    }
    
    
function validateEmail($data, $fieldName) {
    global $errorCount;
    if(empty($data)) {
        echo ""$fieldName" is a required field.<br />n";
        ++$errorCount;
        $retval = "";
    } else {
        $retval = trim($data);
        $retval = stripslashes($retval);
        $pattern = "/^[w-]+(.[w-]+)*@" . "[w-]+(.[w-]+)*" . "(.[[a-z]]{2,})$/i";
        if (preg_match($pattern, $retval)==0) {
            echo ""$fieldName" is not a valid e-mail address.<br />n";
            ++$errorCount;
            }
        }
    return($retval);
}

function displayForm($Sender, $Email, $Subject, $Message) {
?>
<h2 style = "text-align:center">Contact Me</h2>
<form name=contact: action="ContactForm.php" method="post">
<p>Your Name: <input type="text" name="Sender" value="<?php echo $Sender; ?>" /></p>
<p>Your E-mail: <input type="text" name="Email" value="<?php echo $Email; ?>" /></p>
<p>Subject: <input type="text" name="Subject" value="<?php echo $Subject; ?>" /></p>
<p>Message: <br />
<textarea name="Message"><?php echo $Message; ?></textarea></p>
<p><input type="reset" value="Clear Form" />&nbsp;
&nbsp;<input type="submit" name="Submit" value="Send Form" /></p>
</form>
<?php
}

$showForm = TRUE;
$errorCount = 0;
$Sender = "";
$Email = "";
$Subject = "";
$Message = "";

if (isset($_POST['Submit'])) {
    $Sender = validateInput($_POST['Sender'], "Your Name");
    $Email = validateEmail($_POST['Email'], "Your Email");
    $Subject = validateInput($_POST['Subject'], "Subject");
    $Message = validateInput($_POST['Message'], "Message");
    if($errorCount==0)
        $showForm = FALSE;
    else
        $showForm = TRUE;
    }
    
if ($showForm == TRUE) {
    if ($errorCount>0)
        echo "<p>Please re-enter the form information below.</p>n";
        displayForm($Sender, $Email, $Subject, $Message);
}
else {
    $SenderAddress = "$Sender <$Email>";
    $Headers = "From: $SenderAddressn CC: $SenderAddressn";
    $result = mail("greg.englar@gmail.com", $Subject, $Message, $Headers);
    if ($result)
        echo "<p>Your message has been sent. Thank you, " . $Sender . ".</p>n";
    else
        echo "<p>There was an error sending your message, " . $Sender . ".</p>n";
    }
?>
</body> 
 </html>

I think I have a good grasp as to what the code is doing and how it is working but I am clearly missing something. It doesn’t accept any E-Mail as valid and always shows an error message. For reference the book I’m using is “PHP Programming with MySQL Second Edition” by Don Gosselin.

Advertisement

Answer

There’s an error in the regular expression. Should be [a-z] instead of [[a-z]] at the end

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement