如何验证Rails 3.1中的西里尔语电子邮件?

时间:2011-11-18 09:46:10

标签: ruby-on-rails validation

假设我的电子邮件地址为putin-crab@президент.рф

如何在rails 3.1中验证该地址?

我的模特(我使用Mongoid):

#encoding: utf-8
class User
  include Mongoid::Document

  field :email,    :type => String
  validates :email,    :presence => true, :format => { :with => RFC822::EMAIL }
end

对于验证reqexp我使用gem https://github.com/dim/rfc-822

在rails控制台中

普通电子邮件):

ruby-1.9.2-p290 :001 > usr = User.new( :email => "pretty@gmail.com" )
 => #<User _id: 4ec627cf4934db7e4d000001, _type: nil, email: "pretty@gmail.com"> 
ruby-1.9.2-p290 :002 > usr.valid?
 => true 
在rails控制台中

fu @#ing email ):

ruby-1.9.2-p290 :003 > usr = User.new( :email => "putin-crab@президент.рф" )
 => #<User _id: 4ec627f44934db7e4d000002, _type: nil, email: "putin-crab@президент.рф"> 
ruby-1.9.2-p290 :004 > usr.valid?

Encoding::CompatibilityError: incompatible encoding regexp match (ASCII-8BIT regexp with UTF-8 string)
    from /home/username/.rvm/gems/ruby-1.9.2-p290@rail31/gems/activemodel-3.1.1/lib/active_model/validations/format.rb:9:in `=~'
    from /home/username/.rvm/gems/ruby-1.9.2-p290@rail31/gems/activemodel-3.1.1/lib/active_model/validations/format.rb:9:in `!~'
    from /home/username/.rvm/gems/ruby-1.9.2-p290@rail31/gems/activemodel-3.1.1/lib/active_model/validations/format.rb:9:in `validate_each'
    from /home/username/.rvm/gems/ruby-1.9.2-p290@rail31/gems/activemodel-3.1.1/lib/active_model/validator.rb:153:in `block in validate'
    from /home/username/.rvm/gems/ruby-1.9.2-p290@rail31/gems/activemodel-3.1.1/lib/active_model/validator.rb:150:in `each'
    from /home/username/.rvm/gems/ruby-1.9.2-p290@rail31/gems/activemodel-3.1.1/lib/active_model/validator.rb:150:in `validate'
    from /home/username/.rvm/gems/ruby-1.9.2-p290@rail31/gems/activesupport-3.1.1/lib/active_support/callbacks.rb:302:in `_callback_before_13'
    from /home/username/.rvm/gems/ruby-1.9.2-p290@rail31/gems/activesupport-3.1.1/lib/active_support/callbacks.rb:404:in `_run_validate_callbacks'
    from /home/username/.rvm/gems/ruby-1.9.2-p290@rail31/gems/activesupport-3.1.1/lib/active_support/callbacks.rb:81:in `run_callbacks'
    from /home/username/.rvm/gems/ruby-1.9.2-p290@rail31/gems/mongoid-2.3.3/lib/mongoid/callbacks.rb:42:in `block in run_callbacks'
    from /home/username/.rvm/gems/ruby-1.9.2-p290@rail31/gems/mongoid-2.3.3/lib/mongoid/callbacks.rb:67:in `call'
    from /home/username/.rvm/gems/ruby-1.9.2-p290@rail31/gems/mongoid-2.3.3/lib/mongoid/callbacks.rb:67:in `run_cascading_callbacks'
    from /home/username/.rvm/gems/ruby-1.9.2-p290@rail31/gems/mongoid-2.3.3/lib/mongoid/callbacks.rb:41:in `run_callbacks'
    from /home/username/.rvm/gems/ruby-1.9.2-p290@rail31/gems/activemodel-3.1.1/lib/active_model/validations.rb:212:in `run_validations!'
    from /home/username/.rvm/gems/ruby-1.9.2-p290@rail31/gems/activemodel-3.1.1/lib/active_model/validations/callbacks.rb:53:in `block in run_validations!'
    from /home/username/.rvm/gems/ruby-1.9.2-p290@rail31/gems/activesupport-3.1.1/lib/active_support/callbacks.rb:390:in `_run_validation_callbacks'
    from /home/username/.rvm/gems/ruby-1.9.2-p290@rail31/gems/activesupport-3.1.1/lib/active_support/callbacks.rb:81:in `run_callbacks'
    from /home/username/.rvm/gems/ruby-1.9.2-p290@rail31/gems/mongoid-2.3.3/lib/mongoid/callbacks.rb:42:in `block in run_callbacks'
    from /home/username/.rvm/gems/ruby-1.9.2-p290@rail31/gems/mongoid-2.3.3/lib/mongoid/callbacks.rb:67:in `call'
    from /home/username/.rvm/gems/ruby-1.9.2-p290@rail31/gems/mongoid-2.3.3/lib/mongoid/callbacks.rb:67:in `run_cascading_callbacks'
    from /home/username/.rvm/gems/ruby-1.9.2-p290@rail31/gems/mongoid-2.3.3/lib/mongoid/callbacks.rb:41:in `run_callbacks'
    from /home/username/.rvm/gems/ruby-1.9.2-p290@rail31/gems/activemodel-3.1.1/lib/active_model/validations/callbacks.rb:53:in `run_validations!'
    from /home/username/.rvm/gems/ruby-1.9.2-p290@rail31/gems/activemodel-3.1.1/lib/active_model/validations.rb:179:in `valid?'
    from /home/username/.rvm/gems/ruby-1.9.2-p290@rail31/gems/mongoid-2.3.3/lib/mongoid/validations.rb:70:in `valid?'
    from (irb):4
    from /home/username/.rvm/gems/ruby-1.9.2-p290@rail31/gems/railties-3.1.1/lib/rails/commands/console.rb:45:in `start'
    from /home/username/.rvm/gems/ruby-1.9.2-p290@rail31/gems/railties-3.1.1/lib/rails/commands/console.rb:8:in `start'
    from /home/username/.rvm/gems/ruby-1.9.2-p290@rail31/gems/railties-3.1.1/lib/rails/commands.rb:40:in `<top (required)>'
    from script/rails:6:in `require'

1 个答案:

答案 0 :(得分:1)

Regexps无法识别电子邮件,因为它们不是Regular language

有几种很好的方法可以验证Ruby(和Rails)中的电子邮件地址,每种方法都有各种权衡:

  1. 如果您只想验证它是否是正确的格式,请使用专用的特定解析器,如TMail中的解析器,并查看输入是否被接受(解析成功)(不是REGEXP)。例如https://github.com/codyrobbins/active-model-email-validator

  2. 一旦知道电子邮件的格式正确,您就可以更进一步,通过DNS查询“与互联网交谈”,查看电子邮件的域名解析并指定邮件服务器(MX记录)比如https://github.com/Empact/validates_email_veracity_of,虽然这可能太多了。