For some reasons, that are unclear for me, I can't use this code on my website: when I use it the php
form doesn't load.
<?php
$email = "john.doe@example.com";
$email = (filter_var($email, FILTER_SANITIZE_EMAIL));
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
echo("$email is a valid email address");
} else {
echo("$email is not a valid email address");
}
This one works but it doesn't give an error when there isn't a .
after the @
in the email, so the email below get validated
$email = "john.doe@examplecom";
$email = (filter_var($email, FILTER_SANITIZE_EMAIL));
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo("$email is a valid email address");
} else {
echo("$email is not a valid email address");
}
?>
How can I get my form to validate only email with an @
and a .
after the @
Edit:
$authorEmail = (filter_var($_POST['formEmail'], FILTER_SANITIZE_EMAIL));
if (!(filter_var($authorEmail, FILTER_VALIDATE_EMAIL))){
$emailError = true;
$hasError = true;
} else{
$formEmail = $authorEmail;
};
You shouldn't try to enforce validation that is too strict. your second example ("john.doe@examplecom") is a valid email address according to the relevant RFC standards (5321, 5322 and 6531). Other valid email addresses include:
This one has to be posted as code because it contains so many weird characters:
#!$%&'*+-/=?^_`{}|~@example.org
The closest you can get to an RFC compliant home-grown validation system is to use an insanely complex regex: like this one