开发者

Block domain validation?

As part of a User model, I'd like to block certain domains, TLDs, and keywords in email addresses from registering.

For instance, I might want to block any email address that contains qq.com or .pl or hostingcoupons.

There could be dozens domains, TLDs and keywords.

So what would the validator be to do something like that? I'm asking m开发者_开发百科ore on the regex side of things and not so much how to setup a custom validator.

This particular app is Rails 2.3.4.


/\w+(.com|.org|.pl)/ =~ "address@email.com"

\w+ - is any word char one or more times
(.com|.org|.pl) - .com or .org or .pl

This expression will return index of the match or nil if it does not match.
You can refine expression to match exactly the part you are looking for. The other way to do this is to break down the email into regexp groups and evaluate only the group you need e.g. extension.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜