email validation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dru_nasty
Forum Commoner
Posts: 81
Joined: Sat Sep 10, 2005 10:26 am

email validation

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

is there a reason your regular expression is laden with spaces?
dru_nasty
Forum Commoner
Posts: 81
Joined: Sat Sep 10, 2005 10:26 am

Post by dru_nasty »

im not sure what u mean
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

there are lots of spaces in your regular expression. Why?
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post 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 ;))
dru_nasty
Forum Commoner
Posts: 81
Joined: Sat Sep 10, 2005 10:26 am

Post by dru_nasty »

changed my regex to this

Code: Select all

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