form validation email check

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
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

form validation email check

Post by mesz »

Can anybody cast an eye over this...?
I have a form and it works, and the page for writing the text to a file works...with one exception.
The email check (which I blatantly ripped from a tutorial) constatntly returns the failed message.
What stupid logic error have I made?

Code: Select all

<?php 
$fp = fopen('news.txt','a'); 
if ($fp=="") { 
    	echo "Sorry couldn't open file<br>"; 
} else { 
if ($_POST['images']=="") { 
        	echo "<div class=\"button1\">please <a class=\"button1\" href=\"javascript:history.go(-1)\">
        	<b>go back</b></a>and add image link.<BR><i>go on</i>.</div>\n\n"; 
} else { 
if ($_POST['news']=="") { 
        	echo "<div class=\"button1\">please <a class=\"button1\" href=\"javascript:history.go(-1)\">
        	<b>go back</b></a>and add some details.<BR><i>the details are important</i>.</div>\n\n"; 
} else {
if(strstr($_POST["news"],"&")) {
	echo "the text cannot contain the ampersand symbol - &";
} else {
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
	echo "the email address is invalid";
} else {
if(!strstr($_POST["images"],"[img]")) {
	echo "the images link must be formatted correctly<br>ie - contain ''";
}else {
        	$line = date("d.m.y") . "¬" . $_POST['name'] . "¬".$_POST["email"] . "¬".trim($_POST["subject"]). "¬".$_POST["url"]. "¬".$_POST["images"]; 
      	$line .= "¬" . $_POST['news']; 
        	$line = str_replace("\r\n","<BR></BR>",$line); 
       	$line = str_replace(" < "," LESS THAN ",$line); 
        	$line = str_replace(" > "," GREATER THAN ",$line); 
        	$line = str_replace(" >= "," GREATER THAN OR EQUAL TO ",$line); 
        	$line = str_replace(" <= "," LESS THAN OR EQUAL TO ",$line); 
       	$line = str_replace(" & "," and ",$line); 
        	$line = str_replace("&","and",$line); 
       	$line = stripslashes($line); 
        	$line .= "\r\n"; 
        	fwrite($fp, $line); 
	echo "all sorted...cool<br><a href=\"javascript:self.close()\"><small>close window</small></a>";
    } 
} 
}
}
}
}
if(!fclose($fp)) { 	
} 
?>
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Try:

Code: Select all

eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,6}$", $email)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

use preg_match, more efficient

:)
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post by mesz »

cheers guys...but I don't think the problem is actually with the regular expression, but more to do with my flawed logic.
If I use 'eregi' without the '!', the email address always passes the check whether correct or not, whereas if I do include the '!', such as in '!eregi' it always fails.
where is the logic flawed?
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

the logic is fine, the regex probably is not, although i do suck at regex. try this:

Code: Select all

if (!preg_match("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,6}$", $email))
{
  echo 'invalid email';
}
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

when using preg_match, you need to include delimeters :)

Code: Select all

if (preg_match("/^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,6}$/i", $email)) {
    //the address is in valid format
} else {
    //the address is not in valid format
}
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post by mesz »

cheers for the help everybody.
it's sorted...
there was something wrong with that last regex I was given (missing delimiter), but thanks, it pointed me along with the other preg suggestions to finding this preg tutorial which was really helpful.
The final code used is below -

Code: Select all

<?php 
$fp = fopen('news.txt','a'); 
if ($fp=="") { 
    	echo "Sorry couldn't open file<br>"; 
} else { 
if ($_POST['images']=="") { 
        	echo "<div class=\"button1\">please <a class=\"button1\" href=\"javascript:history.go(-1)\">
        	<b>go back</b></a>and add image link.<BR><i>the animals are important</i>.</div>\n\n"; 
} else { 
if ($_POST['news']=="") { 
        	echo "<div class=\"button1\">please <a class=\"button1\" href=\"javascript:history.go(-1)\">
        	<b>go back</b></a>and add some details.<BR><i>the details are important</i>.</div>\n\n"; 
} else {
if(strstr($_POST["news"],"&")) {
	echo "<div class=\"button1\">the text cannot contain the ampersand symbol - &<a class=\"button1\" href=\"javascript:history.go(-1)\">
        	<b>please go back</b></a>and correct.<BR><i>these things are important</i>.</div>\n\n";

} else {
if(!strstr($_POST["images"],"[img]")) {
	echo "<div class=\"button1\">the images link must be formatted correctly<br>ie - contain '<b></b>
	<br><a class=\"button1\" href=\"javascript:history.go(-1)\">
        	<b>please go back</b></a></div>\n\n'";
} else {
if (!preg_match("/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)*\.([a-zA-Z]{2,6})$/", $_POST['email'])) {
   	echo "<div class=\"button1\">invalid e-mail address<br><a class=\"button1\" href=\"javascript:history.go(-1)\">
        	<b>please go back</b></a></div>\n\n";

}else {
        	$line = date("d.m.y") . "¬" . $_POST['name'] . "¬".$_POST["email"] . "¬".trim($_POST["subject"]). "¬".$_POST["url"]. "¬".$_POST["images"]; 
      	$line .= "¬" . $_POST['news']; 
        	$line = str_replace("\r\n","<BR></BR>",$line); 
       	$line = str_replace(" < "," LESS THAN ",$line); 
        	$line = str_replace(" > "," GREATER THAN ",$line); 
        	$line = str_replace(" >= "," GREATER THAN OR EQUAL TO ",$line); 
        	$line = str_replace(" <= "," LESS THAN OR EQUAL TO ",$line); 
       	$line = str_replace(" & "," and ",$line); 
        	$line = str_replace("&","and",$line); 
       	$line = stripslashes($line); 
        	$line .= "\r\n"; 
        	fwrite($fp, $line); 
	echo "all sorted...cool<br>
	<a href=\"javascript:self.close()\"><small>close window</small></a>";
    } 
} 
}
}
}
}
if(!fclose($fp)) { 	
} 
?>
Post Reply