drus40229
2011-07-01 12:53 阅读 73
已采纳

使用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 third 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.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    dthdlv9777 dthdlv9777 2011-07-01 13:06

    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
    
    点赞 评论 复制链接分享
  • dtc88867 dtc88867 2011-07-01 13:01

    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.

    点赞 评论 复制链接分享

相关推荐