Sha256: b682b86f67bc302fa8a2bbe8b58564c9283401d813fb97564732a19fcb3ebe2f
Contents?: true
Size: 990 Bytes
Versions: 4
Compression:
Stored size: 990 Bytes
Contents
# Borrowed from http://my.rails-royce.org/2010/07/21/email-validation-in-ruby-on-rails-without-regexp/ # Mentioned in tweet here: https://twitter.com/_sohara/status/177120126083141633 require 'mail' class EmailValidator < ActiveModel::EachValidator def validate_each(record,attribute,value) begin m = Mail::Address.new(value) # We must check that value contains a domain and that value is an email address r = m.domain && m.address == value t = m.__send__(:tree) # We need to dig into treetop # A valid domain must have dot_atom_text elements size > 1 # user@localhost is excluded # treetop must respond to domain # We exclude valid email values like <user@localhost.com> # Hence we use m.__send__(tree).domain r &&= (t.domain.dot_atom_text.elements.size > 1) rescue Exception => e r = false end record.errors[attribute] << (options[:message] || I18n.t("errors.messages.invalid")) unless r end end
Version data entries
4 entries across 4 versions & 1 rubygems