Reputation: 203
I'm using this inside my user.rb model
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
However, I would like to only allow certain email addresses. Email addresses that end in .org
for example. How can I validate this correctly?
I only want to allow all email addresses that end in .org
that includes @department.xyz.org
, basically any variation that is between @
and .org
Upvotes: 1
Views: 849
Reputation: 20116
(previous answer):
exp = /\A[\w+\-.]+@[a-z\d\-.\.]*xyz\.com\z/i
"[email protected]" =~ exp
=> nil
"[email protected]" =~ exp
=> 0
"[email protected]" =~ exp
=> 0
and to get only the e-mails that finishes with org:
exp = /\A[\w+\-.]+@[a-z\d\-.]+\.org\z/i
=> /\A[\w+\-.]+@[a-z\d\-.]+\.org\z/i
"[email protected]" =~ exp
=> nil
"[email protected]" =~ exp
=> 0
Upvotes: 1