Rick Menss
Rick Menss

Reputation: 107

filter_var in php 5.3.8

I am developing a user registration form and want to validate a user's email address. However,all the php docs I have read suggest the use of filter_var. My script validates a valid email as invalid. Please post a working script or perhaps guide me through my script. Hers is my script :

     <?php
      if(isset($_POST['email'])== true && empty($_POST['email'])false)
      {
       $email = $_POST['email'];
      }
      if(filter_var($email,FILTER_VALIDATE_EMAIL)) 
      {
       echo"valid email";
      }
      else
      {
      echo"invalid email";
      }
     ?>

Upvotes: 3

Views: 413

Answers (4)

NullPoiиteя
NullPoiиteя

Reputation: 57332

if(isset($_POST['email'])== true && empty($_POST['email'])false)

this should be

if(isset($_POST['email']) && !empty($_POST['email']) )

or as @jack you can use just

if (!empty($_POST['email']))

the empty() does implicit isset()

Upvotes: 3

Ja͢ck
Ja͢ck

Reputation: 173662

In your case you shouldn't use filter_var, but filter_input. This is all the code you would need:

if ($email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL)) {
    // email was submitted and is a valid email
}

This bug might be related to your problem though.

Upvotes: 0

Marc B
Marc B

Reputation: 360862

  if(isset($_POST['email'])== true && empty($_POST['email'])false)
                           ^^^^^^^--redundant               ^^^^^---typo?

Upvotes: 0

user669677
user669677

Reputation:

$email = isset($_POST['email']) ? $_POST['email'] : "";
echo(filter_var($email,FILTER_VALIDATE_EMAIL) ? "valid email" : "invalid email");

Upvotes: 2

Related Questions