Page 1 of 1

email validation

Posted: Mon Jan 23, 2006 9:56 pm
by dru_nasty
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?

Code: Select all

<?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.

Posted: Mon Jan 23, 2006 10:00 pm
by feyd
is there a reason your regular expression is laden with spaces?

Posted: Mon Jan 23, 2006 10:10 pm
by dru_nasty
im not sure what u mean

Posted: Mon Jan 23, 2006 10:13 pm
by feyd
there are lots of spaces in your regular expression. Why?

Posted: Mon Jan 23, 2006 11:08 pm
by d3ad1ysp0rk
feyd wrote:there are lots of spaces in your regular expression. Why?
It's like when they take those big tractors and move around piles of trash at the dump. I mean.. there's more space, but it's still a bunch of garbage.


(for anyone who didn't get it, my assumption was for readability ;))

Posted: Mon Jan 23, 2006 11:13 pm
by dru_nasty
changed my regex to this

Code: Select all

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