Tutt 0 Posted May 25, 2011 Report 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 Quote Link to post Share on other sites
Larry 433 Posted May 25, 2011 Report 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. Quote Link to post Share on other sites
phpRob 10 Posted November 7, 2011 Report 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? Quote Link to post Share on other sites
phpRob 10 Posted November 7, 2011 Report Share Posted November 7, 2011 Just found this post which kinda answers my question Quote Link to post Share on other sites
Larry 433 Posted November 7, 2011 Report Share Posted November 7, 2011 Yep. Let me know if you'd like further clarification. Quote Link to post Share on other sites
phpRob 10 Posted November 7, 2011 Report 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! Quote Link to post Share on other sites
Larry 433 Posted November 7, 2011 Report 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). Quote Link to post Share on other sites
phpRob 10 Posted November 7, 2011 Report 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! Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.