PDA

View Full Version : email validation



dru_nasty
January 23rd, 2006, 10:56 PM
I've got the following script that runs after a form is submitted.
Every part of it works, except one thing.
When I test the form by entering an invalid email address, i get the echo "you have to enter a valid email."

But when I enter a valid email, it still gives me the same error echo.

What am i missing?



<?php

$to = "myemailaddress@domain.com";
$subject = "Quick Contact Form Submission";
$firstname_field = $_POST['firstname'];
$lastname_field = $_POST['lastname'];
$email_field = $_POST['email'];
$message_field = $_POST['message'];
$body = "First: $firstname_field\n Last: $lastname_field\n E-Mail: $email_field\n Message: $message_field\n";

if(!isset($_POST['submit'])) {
header( "Location: http://www.bluetorchmedia.com/contact.php" );

}elseif (empty($firstname_field) || empty($lastname_field)) {
echo "you have to fill out everything";


}elseif (!preg_match("/^( [a-zA-Z0-9] )+( [a-zA-Z0-9\._-] )*@( [a-zA-Z0-9_-] )+( [a-zA-Z0-9\._-] +)+$/" , $email_field)) {
echo "you have to enter a valid email address";

}else{

mail($to, $subject, $body);
?>

<html>
<head>
</head>

<body>
Going to show stuff here.
<?
}
?>
</body>

</html>


So it basically checks to make sure the user got to this script by hitting the submit button on my form.

Then it makes sure certain fields were filled out.

Then it checks if the email is valid, if not then echo the error, if it's good send the mail() function and display the html.

If the email is valid it still shows the error echo only.

Please help.

hl
January 24th, 2006, 12:05 AM
your regular expression is incorrect is all. i haven't dealt much with regex, but www.regular-expressions.info

i believe they have a regex for an email

dru_nasty
January 24th, 2006, 12:19 AM
your regular expression is incorrect is all. i haven't dealt much with regex, but www.regular-expressions.info

i believe they have a regex for an email

Good call harish, I changed my regex to this


'/^[.\w-]+@([\w-]+\.)+[a-zA-Z]{2,6}$/'

and it did the trick

hl
January 24th, 2006, 05:29 PM
Good call harish, I changed my regex to this


'/^[.\w-]+@([\w-]+\.)+[a-zA-Z]{2,6}$/'

and it did the trick
you're welcome :)