Robmeister2015
Robmeister2015

Reputation: 229

filter_validate_email not working or even being detected?

I've tried all the other answers for my query on here and none of them suited my needs. I'm on PHP 5.2, and I'm starting to think that maybe this filter doesn't work on my version? I've added code below, let me know if you see anything. I've tried it with and without sanitizing, and every time it just skips over the entire clause and submits the form to the database.

if(isset($_POST['fname']) && !empty($_POST['fname']) AND isset($_POST['lname']) && !empty($_POST['lname']) AND isset($_POST['college']) && !empty($_POST['college']) AND isset($_POST['address']) && !empty($_POST['address']) AND isset($_POST['username']) && !empty($_POST['username']) AND isset($_POST['password']) && !empty($_POST['password']) AND $passwordEmail == $password2){

$address = $_POST['address'];

$address = filter_var($address, FILTER_SANITIZE_EMAIL);

 if(!filter_var($address, FILTER_VALIDATE_EMAIL) === false) {
header("location: registration.php?remarks=successEmail&college=$college&fname=$fname&lname=$lname&contact=$contact&username=$username");
}

else{
header("location: registration.php?remarks=failedEmail&college=$college&fname=$fname&lname=$lname&contact=$contact&username=$username");
}

Edit:

I removed the duplicate function and the ! and it still does not work.

The only addition was the moving of the '!' and the removal of the first filter.

This is the code I am now using which is still failing and taken from some of the answers given:

if(isset($_POST['fname']) && !empty($_POST['fname']) AND isset($_POST['lname']) && !empty($_POST['lname']) AND isset($_POST['college']) && !empty($_POST['college']) AND isset($_POST['address']) && !empty($_POST['address']) AND isset($_POST['username']) && !empty($_POST['username']) AND isset($_POST['password']) && !empty($_POST['password']) AND $passwordEmail == $password2){

$address = $_POST['address'];

 if(filter_var($address, FILTER_VALIDATE_EMAIL) != false) {
header("location: registration.php?remarks=successEmail&college=$college&fname=$fname&lname=$lname&contact=$contact&username=$username");
}

else{
header("location: registration.php?remarks=failedEmail&college=$college&fname=$fname&lname=$lname&contact=$contact&username=$username");
}

Final Edit: I copied this example I found and just changed the variable names and now it works.

<?php
$email = "[email protected]";

if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
  echo("$email is a valid email address");
} else {
  echo("$email is not a valid email address");
}
?> 

Upvotes: 1

Views: 2502

Answers (3)

Pascal van Loon
Pascal van Loon

Reputation: 1

I had the same problem. What I found out was that the else was below the }. When I moved it right behind the } the validation worked.

Wrong code:

    if(filter_var($em, FILTER_VALIDATE_EMAIL)){

            $em = filter_var($em, FILTER_VALIDATE_EMAIL);

   }

    else{
        echo "Invalid email format";
    }

Working code:

    if(filter_var($em, FILTER_VALIDATE_EMAIL)){

            $em = filter_var($em, FILTER_VALIDATE_EMAIL);

            //Check if email already exists

    }else{
        echo "Invalid email format";
    }

Upvotes: 0

b14r
b14r

Reputation: 362

You are using filter_var TWICE thats why it will always fail doing this

if(!filter_var($address, FILTER_VALIDATE_EMAIL) === false) {

The variable $address has already been modified to a boolean value in the line before:

$address = filter_var($address, FILTER_SANITIZE_EMAIL);

$address has the false or true value instead of the email string!

Oh, and of course, like John Conde said, you're using the ! in the wrong place.

Upvotes: 1

John Conde
John Conde

Reputation: 219804

Your ! is in the wrong place.

if(filter_var($address, FILTER_VALIDATE_EMAIL) !== false) {

By putting it before the function name you are saying "if the opposite of the return value of filter_var() is equal to false and the same type". That gives you true === false which of course is false.

What you really mean is "if the return value of filter_var() is equal to false and the same type". That gives you true !== false which is of course true.

Or simplify it:

if(filter_var($address, FILTER_VALIDATE_EMAIL)) {

Upvotes: 5

Related Questions