开发者

Block domain validation?

开发者 https://www.devze.com 2023-04-03 21:57 出处:网络
As part of a User model, I\'d like to block certain domains, TLDs, and keywords in email addresses from registering.

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

精彩评论

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