NYCPHP Meetup

NYPHP.org

[nycphp-talk] Preg_match email address

David Sklar sklar at sklar.com
Mon Sep 15 11:32:31 EDT 2003


On Monday, September 15, 2003 10:56 AM,  wrote:

> I'm hoping those who are more adept at regular expressions could tell
> me if this will validate the majority of email addresses...it seems
> like it will but...
>
> '/^[ a-z0-9-_]+(.[
> a-z0-9-_]+)*@[a-z0-9-]+(.[a-z0-9-_]+)*(.[a-z]{2,3})$/i',

This regex is too broad. You need to escape the periods in the regex so they
match just ".", not any character. You allow usernames that begin with "
" -- do you really want that? The regex also allows domain names that begin
with "-", e.g. foo at -bar.com, which are not valid. Some mail servers allow
individual users to create aliases by appending their username with a "+",
i.e. "foo+list at bar.com", "foo+shop at bar.com" -- your regex does not allow +
signs in usernames.

I know this has come up on the list before, but I'll put in a recommendation
for using the techniques discussed in Recipe 13.5 in PHP Cookbook. There are
two regexes there and some other techniques for verifying addresses, along
with the plusses and minuses of each. If you don't own a copy of PHP
Cookbook (why not?!:) you can download the example code from
http://examples.oreilly.com/phpckbk/phpcbbk_examples.zip

David




More information about the talk mailing list