views:

68

answers:

3

After reading various posts I decided not to use REGEX to check if an email is valid and simply use PHP's inbuilt filter_var function. It seemed to work ok, until it started telling me an email was invalid because I had a number in it.

ie [email protected] works, while [email protected] doesn't.

Am I missing something or is the filter_var($email, FILTER_VALIDATE_EMAIL) really quite ineffective?

+1  A: 

[email protected] seems to work fine: http://codepad.org/5HDgMW5i

But I've definitely seen people complaining it's got problems, even on SO. In all likelihood, it does have problems, but so will a regex solution. The email address specifications are very, very complicated (RFC XXXX).

That's why the only solution to verify emails you should rely on is sending an email to the address and demand action (eg: if it's a registration script ask them to click on a verification link).

NullUserException
Thanks for that. I will be sending a validation email it would just be nice to have something in place to make sure people don't accidentally throw in obviously wrong characters.
KeenLearner
@Keen I am not saying throw out regex validation out the window; you could validate it and if it fails warn the user (eg: "the computer says your email is invalid, but it ain't very smart. are you sure you want to use this email?")
NullUserException
Thats a great idea!
KeenLearner
+1  A: 

that filter has been revamped recently. http://codepad.org/Lz5m2S2N - appears that in version used by codepad your case is filtered correctly

You can also look at http://bugs.php.net/49576 and http://svn.php.net/viewvc/php/php-src/trunk/ext/filter/logical_filters.c . Regexp is quite scary.

ts
+2  A: 

The regular expression used in the PHP 5.3.3 filter code is based on Michael Rushton's blog about Email Address Validation. It does seem to work for the case you mention.

You could also check out some of the options in Comparing E-mail Address Validating Regular Expressions (the regexp currently used in PHP is one of those tested).

Then you could choose a regexp you like better, and use it in a call to preg_match().

Or else you could take the regexp and replace the one in file PHP/ext/filter/logical_filter.c, function php_filter_validate_email(), and rebuild PHP.

Bill Karwin
That about covers it :)
Andy