Jump to content

Validating using filter_var


Recommended Posts

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

Link to comment
Share on other sites

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.';
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by grissom
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.