Tutt Posted May 25, 2011 Share Posted May 25, 2011 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 More sharing options...
Larry Posted May 25, 2011 Share Posted May 25, 2011 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 More sharing options...
phpRob Posted November 7, 2011 Share Posted November 7, 2011 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 More sharing options...
phpRob Posted November 7, 2011 Share Posted November 7, 2011 Just found this post which kinda answers my question Link to comment Share on other sites More sharing options...
Larry Posted November 7, 2011 Share Posted November 7, 2011 Yep. Let me know if you'd like further clarification. Link to comment Share on other sites More sharing options...
phpRob Posted November 7, 2011 Share Posted November 7, 2011 From my basic knowledge and brief reading isset() checks to see if a value has been set and empty() checks to see if a variable is empty, 0 or NULL? Please correct me if I'm wrong Larry! Link to comment Share on other sites More sharing options...
Larry Posted November 7, 2011 Share Posted November 7, 2011 Yes, that's correct. And the main distinction in terms of form validation is that a variable can be set but still empty (e.g., an empty string). Link to comment Share on other sites More sharing options...
phpRob Posted November 7, 2011 Share Posted November 7, 2011 Yes, that's correct. And the main distinction in terms of form validation is that a variable can be set but still empty (e.g., an empty string). PHP might actually be sinking in after all hey Larry! Link to comment Share on other sites More sharing options...
Recommended Posts