Jump to content
Larry Ullman's Book Forums

Validate Email Address With Filter_Var


Recommended Posts

I would like to see an example of the filter_var() used to validate an email address in register.php from Chapter 8 Review and Pursue. I've tried a million things but it's not catching errors correctly so I think my IF conditional is off.

 

// original code for email validation:

if (empty($_POST['email']) || (substr_count($_POST['email'], '@') != 1) ) {

$problem = TRUE;

print '<p class="error">Please enter your email address!</p>';

}

 

Thank you!

Jill

Link to comment
Share on other sites

Sure, that'd be:

if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
   $problem = TRUE;
   // Etc.

 

I'd be inclined to add an isset() clause to that conditional, too, but that's a minor point.

Link to comment
Share on other sites

  • 5 months later...

I'm still a little confused understanding the differences between the empty() or isset() functions. In the above you suggest using the isset() clause on the 'email' variable, would it not work with empty()?. Think I remember reading you'd use isset on checkboxes, radio buttons alike.

 

Please explain?

Link to comment
Share on other sites

 Share

×
×
  • Create New...