I use both Javascript on the client side to validate if it's an email address, and PHP on the backend to check if it's valid. You'll basically be able to search for the @ and . with the strings in between.
I don't have any code off hand, but it's not bad. Just grab the form field by ID in Javascript, put it into a variable, and run a check on what is actually in the variable. If there's 'something'@'something'.'something' return true, else return false. Maybe someone else has some code for you.
// assume form field name is "email":
$email = trim($_POST['email']); // get rid of leading/trailing whitespace
if(empty($email))
{
// handle error here (display error message, or return to form via header(), etc.)
}
else
{
// proceed with form processing
}
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
EDIT: Hrm, this is actually way overkill for what you want. If you just want to verify the field is not empty, then NogDog nailed it for you.
Error checking e-mail is not fun. This is by no means perfect, but its a start (should be used after you've checked to make sure the field is not empty).
if (!eregi($validEmailExpr, $_SESSION["{$formVars}"]["{$field}"]))
{
$_SESSION["{$errors}"]["{$field}"] =
"The email must be in the name@domain format.";
return false;
}
I did not write this originally, just a snippet I'd stored at some point. Another option would be to verify the the e-mail domain exists by checking the MX record or just doing a hostname lookup.
This is a very complete interpretation of the RFC822 email address specification, and should give no false positives or false negatives (as far as format goes, not whether the address actually exists).
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Bookmarks