Page 1 of 1
can you help with 'if' statement
Posted: Sat Jan 16, 2010 9:52 am
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

Re: can you help with 'if' statement
Posted: Sat Jan 16, 2010 10:17 am
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>";}
}
?>
Re: can you help with 'if' statement
Posted: Sat Jan 16, 2010 10:25 am
by me666
excellent thank u very much

Re: can you help with 'if' statement
Posted: Sat Jan 16, 2010 2:17 pm
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().
Re: can you help with 'if' statement
Posted: Sun Jan 17, 2010 6:29 am
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
Re: can you help with 'if' statement
Posted: Sun Jan 17, 2010 2:09 pm
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.
Re: can you help with 'if' statement
Posted: Mon Jan 18, 2010 9:46 am
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
