can you help with 'if' statement

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
me666
Forum Commoner
Posts: 87
Joined: Wed Oct 08, 2008 5:04 pm

can you help with 'if' statement

Post by me666 »

hi all, ive got a form that asks the user to enter certain info and saves it in a table... when a text field is left empty an error will come up using an if statement, but now i would like an error to come up if no '@' is found in the email field....
the code i use for empty fields is..
if($Email == null){
Die (print"Please enter a valid email address");
}

thanks :D
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: can you help with 'if' statement

Post by JakeJ »

Here's a regular expression that will validate an email address as being legitimate. Adapt to your needs (taking out the echos for example).

Code: Select all

 
<?php
if(isset($todo) and $todo=="test"){
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
echo "<center>Invalid email</center>";
}else{
echo "<center>Valid Email</center>";}
}
?>
 
me666
Forum Commoner
Posts: 87
Joined: Wed Oct 08, 2008 5:04 pm

Re: can you help with 'if' statement

Post by me666 »

excellent thank u very much
:D
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: can you help with 'if' statement

Post by AbraCadaver »

JakeJ wrote:Here's a regular expression that will validate an email address as being legitimate. Adapt to your needs (taking out the echos for example).

Code: Select all

 
<?php
if(isset($todo) and $todo=="test"){
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
echo "<center>Invalid email</center>";
}else{
echo "<center>Valid Email</center>";}
}
?>
 
eregi() is deprecated, so I would use preg_match(). There is also a built in PHP filter for email. Check filter_var().
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
me666
Forum Commoner
Posts: 87
Joined: Wed Oct 08, 2008 5:04 pm

Re: can you help with 'if' statement

Post by me666 »

thank u both 4 the help, but i must have an error some where.. this is the code i'm using

Code: Select all

if(isset($_POST['sign-up'])){
if(preg_match('/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@([a-z0-9])*(\.([a-z0-9])([-a-z0-9_-])([a-z0-9])+)*$/i','$Email')){
}
Else
{Die (sorry invalid email)
}
<form action="<? echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post">
First Name:</td><td align="left"><input type="text" length="50" name="FName">
</td></tr>
<tr><td align="right">Last Name:</td><td align="left"><input type="text" length="50" name="LName">
</td></tr>
<tr><td align="right">Email:</td><td align="left"><input type="text" length="50" name="Email">
</td></tr>
<tr><td align="right">Password:</td><td align="left"><input type="password" length="50" name="Pass">
</td></tr>
<tr><td colspan="2" align="center"><input type="submit" name="sign-up" value="Sign Up"></form>
And this is the error i get..
Parse error: parse error, unexpected T_STRING in /home/www/website-address/index.php on line 14
Line 14 points to

Code: Select all

if(preg_match('/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@([a-z0-9])*(\.([a-z0-9])([-a-z0-9_-])([a-z0-9])+)*$/i','$Email')){
thanks

i also get same error using the first method sujested
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: can you help with 'if' statement

Post by JakeJ »

Your error is actually in line 5.

try die("invalid email");

You're missing quotes and the semi-colon.

Also, when you post code, it helps if you insert <?php to start and ?> to end so that it shows up with proper formatting.
me666
Forum Commoner
Posts: 87
Joined: Wed Oct 08, 2008 5:04 pm

Re: can you help with 'if' statement

Post by me666 »

no that was just a snippet of code line 14 was the 'preg_match'...
but i think it may of had something to do with using php4? i have updated my code so i can now use php5 and it works ok with the built in email verification tool... i have also added the quotes ond semi colons now too, thanks for the help and sujestions :D
Post Reply