Emails are not validating with filter_var

46 Views Asked by At

I'm making now registration for my site. I have made validation.php file in which i have all functions that are validating my data. Email is validated like this:

if (filter_var($string, FILTER_VALIDATE_EMAIL) == false) {
    $allChecked = false;
    $_SESSION['emailError'] = "Your email has to be valid.";
}

But when i click on button (in registration form) when email is invalid (random letters) is gets through validation like the email is okay. So how i have to this so the email gets properly validated. Thanks for every answer

1

There are 1 best solutions below

0
Nancy Moore On

Try this code below.

It will allow good email but will prevent in valid email

        $email= 'nancy@gmai';  // bad email
    /*
    $email= '[email protected]';  //good email
    */
        $em= filter_var($email, FILTER_VALIDATE_EMAIL);
        if (!$em){
        echo "Email Address is Invalid";
 //$_SESSION['emailError'] = "Your email has to be valid.";

        exit();
        }