I'm doing a form check and part of this is checking for mandatory fields. One field however is an email field that I'm checking for proper email structure ONLY if the field isn't left blank.
The code I'm using is turning an error message even if the email field is left blank.
Here's the original code:
- if (eregi ('^[[:alnum:]][a-z0-9_\.\-]*@[a-z0-9\.\-]+\.[a-z]{2,4}$', stripslashes(trim($_POST['email']))))
-
{
-
$e = escape_data($_POST['email']);
-
}
-
else
-
{
-
$e = FALSE;
-
echo "<tr><td align='left'><span style='font-size:14px; color:red; font-weight:bold;'>Please enter a valid email address.</span></td></tr>";
-
}
And here's what I've tried but doesn't work:
- if (!empty($_POST['email']))
-
{
-
if (eregi ('^[[:alnum:]][a-z0-9_\.\-]*@[a-z0-9\.\-]+\.[a-z]{2,4}$', stripslashes(trim($_POST['email']))))
-
{
-
$e = escape_data($_POST['email']);
-
}
-
else
-
{
-
$e = FALSE;
-
echo "<tr><td align='left'><span style='font-size:14px; color:red; font-weight:bold;'>Please enter a valid email address.</span></td></tr>";
-
}
-
}
How can I write this code so that if the email field is empty it will just ignore the stupid thing and move on, and only perform the email structure check if something has been put into the email field?