The condition to validate the email does not work

Viewed 22

in my conditions I test in order to know if the email variable then I would like to know if the user has entered a valid email But my code does not return me an error if the user enters a false email

......
// je verifie si la variable est bien remplie 
elseif (!isset($_POST["ville"]) || ($_POST["ville"]==""))
    {echo "0-5"; } 

// je verifie si la variable est bien remplie 
elseif (!isset($_POST["pays"]) || ($_POST["pays"]==""))
    {echo "0-6"; } 

// je verifie si la variable est bien remplie 
elseif (!isset($_POST["email"]) || ($_POST["email"]=="")){
    
//si la variable est remplie je regarde si c'est une vraie adresse email
    if (filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)){
    echo "L'adresse e-mail est valide";
}else{
    echo "0-7"; }
}

1 Answers

You have done the opposite of what you need. Currently your code is not checking the validity of email because the condition is not letting to check it.

You need to check if email is being posted and if it's not empty, than you can check if it's a valid email or not.

// je verifie si la variable est bien remplie 
elseif (isset($_POST["email"]) && !empty($_POST["email"]){
    
//si la variable est remplie je regarde si c'est une vraie adresse email
    if (filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)){
    echo "Email is valid";
}else{
    echo "You have to enter valid email."; }
}
Related