在Ruby中验证电子邮件地址的最佳/简便方法是什么?

验证红宝石电子邮件地址(在服务器端)的最佳/简便方法是什么?

Ran asked 2020-02-13T14:48:05Z
10个解决方案
62 votes

您可以查看它是否与正则表达式匹配,例如此Rails验证器中使用的正则表达式:

validates_format_of :email,:with => /\A[^@\s]+@([^@\s]+\.)+[^@\s]+\z/

但是,如果您使用Devise,只需执行以下操作:

validates_format_of :email,:with => Devise::email_regexp

资料来源:[http://lindsaar.net/2008/4/14/tip-4-detecting-a-valid-email-address]

编辑1:

有用的测试网站:[http://www.rubular.com/]

apneadiving answered 2020-02-13T14:48:30Z
8 votes

在Ruby中? 与任何语言相同的方式。

将确认电子邮件发送到该地址,其中包含收件人必须单击的链接,然后该电子邮件地址才被视为完全有效。

有多种原因导致格式正确的地址可能仍然无效(该地址没有实际用户,被垃圾邮件过滤器阻止,依此类推)。 唯一可以确定的方法是成功完成某些描述的端到端事务。

paxdiablo answered 2020-02-13T14:49:00Z
6 votes
validates :email, presence: true, format: /\w+@\w+\.{1}[a-zA-Z]{2,}/

检查电子邮件字段是否为空白,并且在“ @”之前和之后都包含一个或多个字符

增加了特殊性,在@之前的任何1个或多个单词字符,以及在1 .之后和之间的任意1个或多个单词字符,以及之后的至少2个字母

partydog answered 2020-02-13T14:49:25Z
5 votes

我知道这是一个老问题,但我一直在寻找一种简单的方法来做到这一点。 我遇到了一个email_validator gem,它的设置和使用非常简单。

作为验证者

EmailValidator.valid?('narf@example.com') # boolean

模型外验证

EmailValidator.valid?('narf@example.com') # boolean

希望对大家有帮助。

快乐的外衣

MZaragoza answered 2020-02-13T14:50:15Z
4 votes

您可以使用

<%=email_field_tag 'to[]','' ,:placeholder=>"Type an email address",:pattern=>"^([\w+-.%]+@[\w-.]+\.[A-Za-z]{2,4},*[\W]*)+$",:multiple => true%>
akshay answered 2020-02-13T14:50:34Z
3 votes

快捷方式:

 validates :email, :format => /@/

正规表格(正则表达式):

validates :email, :format => { :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/ }

来源:验证器类

Omer Aslam answered 2020-02-13T14:51:03Z
3 votes

由于主要答案的博客站点已关闭,因此以下是该站点通过不错的缓存器或摘要提供的代码段:

# http://my.rails-royce.org/2010/07/21/email-validation-in-ruby-on-rails-without-regexp/
class EmailValidator < ActiveModel::EachValidator
  # Domain must be present and have two or more parts.
  def validate_each(record, attribute, value)
    address = Mail::Address.new value
    record.errors[attribute] << (options[:message] || 'is invalid') unless (address.address == value && address.domain && address.__send__(:tree).domain.dot_atom_text.elements.size > 1 rescue false)
  end
end
toobulkeh answered 2020-02-13T14:51:25Z
3 votes

您可以从[https://apidock.com/rails/ActiveModel/Validations/HelperMethods/validates_format_of]中获取参考

validates_format_of :email, with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i

Kanmaniselvan answered 2020-02-13T14:51:50Z
2 votes

发送确认邮件,我通常会使用此验证器... D.R.Y.

# lib/email_validator.rb
class EmailValidator < ActiveModel::EachValidator

  EmailAddress = begin
    qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]'
    dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]'
    atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-' +
      '\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+'
    quoted_pair = '\\x5c[\\x00-\\x7f]'
    domain_literal = "\\x5b(?:#{dtext}|#{quoted_pair})*\\x5d"
    quoted_string = "\\x22(?:#{qtext}|#{quoted_pair})*\\x22"
    domain_ref = atom
    sub_domain = "(?:#{domain_ref}|#{domain_literal})"
    word = "(?:#{atom}|#{quoted_string})"
    domain = "#{sub_domain}(?:\\x2e#{sub_domain})*"
    local_part = "#{word}(?:\\x2e#{word})*"
    addr_spec = "#{local_part}\\x40#{domain}"
    pattern = /\A#{addr_spec}\z/
  end

  def validate_each(record, attribute, value)
    unless value =~ EmailAddress
      record.errors[attribute] << (options[:message] || "is not valid") 
    end
  end

end

在您的模型中

validates :email , :email => true

要么

 validates :email, :presence => true, 
                :length => {:minimum => 3, :maximum => 254},
                :uniqueness => true,
                :email => true
andrea answered 2020-02-13T14:52:19Z
2 votes

如果您使用的是Rails / Devise-除了@apneadiving的答案-

validates_format_of :email,:with => Devise::email_regexp

Devise :: email_regexp来自config / initializers / devise.rb

config.email_regexp = /\A[^@\s]+@([^@\s]+\.)+[^@\s]+\z/
Yurii Verbytskyi answered 2020-02-13T14:52:44Z
translate from https://stackoverflow.com:/questions/4776907/what-is-the-best-easy-way-to-validate-an-email-address-in-ruby