![](/img/trans.png)
[英]How can I configure my app to allow only specific emails to register with devise?
[英]Allow only specific domain names to register in Rails App (Devise gem)
我在這里已閱讀過類似的案例: 驗證特定的電子郵件特定域設計了3 Ruby on Rails 4
但是,它只是一個電子郵件域(例如example.com)。 我想要的是允許我擁有一些電子郵件域(例如domain1.com,domain2.com,domain3.com等)。 如何實現呢?
您可以在模型中添加以下代碼
validate :email_domain
def email_domain
domain = email.split("@").last
if !email.blank?
errors.add(:email, "Invalid Domain") if domain != "gmail.com"
end
end
您可以添加任意數量的域。
我在其他線程中使用了Pavan的代碼( 僅允許特定的電子郵件在Rails應用程序(Devise)中注冊 ),並且可以正常工作。
為了允許特定的電子郵件,我將以下代碼用於用戶模型:
validates :email, format: { with: /\b[A-Z0-9._%a-z\-]+@xyz\.com\z/, message: "must be a xyz.com account" }
APPROVED_DOMAINS = ["domain_one.com", "domain2.com"]
* or you can load this from somewhere else
validates :email, presence: true, if: :domain_check
def domain_check
unless APPROVED_DOMAINS.any? { |word| email.end_with?(word)}
errors.add(:email, "is not from a valid domain")
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.