开发者

Block certain email providers using Regex

Please dont downvote the question because of the fact that the answer Im looking for is not an anser someone should pursue. I'm fully aware of that, but it's not my idea, I just have to deliver :D

In cakephp, I have the following dataentry in my model:

    'email' => array(
        'email' => array(
            'rule' => array('email',false,'(^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$)')
        ),
    )

The email rule is a common function in cakephp data validation, and the second and third parameter are optional. The t开发者_StackOverflow社区hird being the regex. I wasnt happy with the given regex string so I added my own. Now I want to exclude Gmail, Hotmail and yahoo addresses.

How can I change the Regular Expression so those addresses are producing false as result? I cant get it right.


Why on earth would you want to exclude gmail, hotmail and yahoo addresses? There are plenty of people who only have one of these addresses and no other. This is a bad idea. If you are target a specific "audience" I'd suggest making a list of allowed domains instead.

Anyway, here's a functional regex for you which is shorter than the one you already have.. try it out:

\b[\w\.-]+@((?!gmail|googlemail|yahoo|hotmail).)[\w\.-]+\.\w{2,4}\b


Don't use a regex for this.

The proper solution is to explode() the email address at the @ sign and then use plain string comparisons or even in_array() to check if the domain is blacklisted.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜