Rails,验证电子邮件排除

use*_*805 3 email validation ruby-on-rails blacklist

我目前正在尝试使用一些东西来验证电子邮件属性:

  1. 它的存在
  2. 它的格式与正则表达式
  3. 它的独特性
  4. 它不存在于邮件提供商列表中

我陷入了第四步,我真的不知道如何实现它,这个步骤的主要部分是排除可抛出的邮件提供者.

我现在这个:

  validates :email, :presence   => true,
                    :format     => { :with => email_regex },
                    :uniqueness => { :case_sensitive => false },
                    :exclude => Not working when I put a regex here
Run Code Online (Sandbox Code Playgroud)

我的问题不是正则表达式,而是如何排除与排除正则表达式的电子邮件匹配.

你能帮帮我吗?

亲切的,罗布.

Hto*_*ung 8

如果您使用devise进行用户身份验证,则可以取消注释devise.rb中的代码

  # Email regex used to validate email formats. It simply asserts that
  # one (and only one) @ exists in the given string. This is mainly
  # to give user feedback and not to assert the e-mail validity.
  # config.email_regexp = /\A[^@]+@[^@]+\z/
Run Code Online (Sandbox Code Playgroud)

否则我觉得你可以这样写

在模型中

  validates :email, uniqueness: true
  validate  :email_regex

 def email_regex
    if email.present? and not email.match(/\A[^@]+@[^@]+\z/)
      errors.add :email, "This is not a valid email format"
    end
  end
Run Code Online (Sandbox Code Playgroud)


iwi*_*nia 5

格式验证器有一个无选项(至少在rails 4和3.2中),所以......

validates :email, :presence   => true,
                  :format     => { :with => email_regex},
                  :uniqueness => { :case_sensitive => false }
validates :email, :format     => {:without => some_regex}
Run Code Online (Sandbox Code Playgroud)