NotionCommotion Posted June 1, 2015 Share Posted June 1, 2015 A while ago, I created a validation class which includes a bunch of methods to validate various types of data. For instance, the method to validate an email looks like: protected function email($x,$true,$name) { //$name isn't used with the email method, but is used with other methods to return a response like "$name is not a bla" return (!$true || !trim($x) || preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i', $x) )?false:'Invalid email.'; } I recently started looking into http://php.net/manual/en/book.filter.php. Looks like I could do the same thing using: protected function email($x,$true,$name) { return (!$true || !trim($x) || filter_var($x, FILTER_VALIDATE_EMAIL) )?false:'Invalid email.'; } Assuming I am running PHP version 5.5.24, Should I be using the extention all times when available? Anything I should keep an eye out for? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/296586-validating-using-filter_var/ Share on other sites More sharing options...
Ch0cu3r Posted June 1, 2015 Share Posted June 1, 2015 Yes you can use filter_var for validating user input, PHP has built in standard filters for validating user data listed here: http://php.net/manual/en/filter.filters.validate.php What i do not understand about your code is this protected function email($x,$true,$name) { //$name isn't used with the email method, but is used with other methods to return a response like "$name is not a bla" Why have $name as an argument if you are not going to be using it? I'm struggling to understand whats happening here. Whats with the !$true and !trim() and why return false when an email is valid? return (!$true || !trim($x) || filter_var($x, FILTER_VALIDATE_EMAIL) )?false:'Invalid email.'; Quote Link to comment https://forums.phpfreaks.com/topic/296586-validating-using-filter_var/#findComment-1512980 Share on other sites More sharing options...
NotionCommotion Posted June 1, 2015 Author Share Posted June 1, 2015 Hi ChOcu3r. So, I "could", and probably "should" use filter_var, correct? Are there any methods that are considered buggy? As I tried to example, $name is only used with other methods such as the following three: protected function required($x,$true,$name) { return (!$true || trim($x))?false:$name.' is required.'; } protected function minlength($x,$minlength,$name) { return (trim($x) && strlen(trim($x))<$minlength)?$name.' requires '.$minlength.' characters.':false; } protected function maxlength($x,$maxlength,$name) { return (trim($x) && strlen(trim($x))>$maxlength)?$name.' allows no more than '.$maxlength.' characters.':false; } In regards to !$true and !trim(), the first arguement follows jQuery validator plugin structure, and !trim() prevents it from validating as false if the variable is empty. Returning false if the variable validates allows a non-false response (i.e. the message) to be returned. Quote Link to comment https://forums.phpfreaks.com/topic/296586-validating-using-filter_var/#findComment-1512982 Share on other sites More sharing options...
Ch0cu3r Posted June 1, 2015 Share Posted June 1, 2015 It is up to you whether or not use the built in filters. You are free to write your own if you wish to do so. As I tried to example, $name is only used with other methods such as the following three: Thats fine for those methods. My comment was referring to your code comment for the email method it says $name is never used. Thats why I said if you are not going to use $name as an argument then there is no point in defining it as a argument for your email method Quote Link to comment https://forums.phpfreaks.com/topic/296586-validating-using-filter_var/#findComment-1512987 Share on other sites More sharing options...
grissom Posted June 1, 2015 Share Posted June 1, 2015 (edited) No method of validating an e-mail is entirely perfect, but tbh FILTER_VALIDATE_EMAIL is easily likely to be equally as good as any REGEX ideas you'll find on Google (and there are loads of them, as I'm sure you've seen already !) As I understand it, though, as with many other solutions, it is simply checking that the "pattern" of characters that make up the email address conform to the specified rules. It does not test (if I understand it right) that the email address is actually real, nor is there any checking to do with disposable e-mail accounts. In the validation function I wrote for myself, I also included a test for all the popular disposable e-mail accounts (dispostable, guerilla, mailinator etc.). Edited June 1, 2015 by grissom Quote Link to comment https://forums.phpfreaks.com/topic/296586-validating-using-filter_var/#findComment-1512994 Share on other sites More sharing options...
NotionCommotion Posted June 1, 2015 Author Share Posted June 1, 2015 Thanks Grissom, Yes, there are many, many, many solutions out there! One benefit I like about FILTER_VALIDATE_EMAIL is that it will be updated whenever I update my PHP version. I agree it will not validate whether the email is valid, and tested so just to be certain. Good point about testing for popular disposable e-mail accounts. Quote Link to comment https://forums.phpfreaks.com/topic/296586-validating-using-filter_var/#findComment-1512995 Share on other sites More sharing options...
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.