Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saturnflyer/2ead89f0cb8b5b1f1d41 to your computer and use it in GitHub Desktop.
Save saturnflyer/2ead89f0cb8b5b1f1d41 to your computer and use it in GitHub Desktop.
require 'resolv'
class EmailValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
if Resolv::DNS.new.getresources(value.split("@").last, Resolv::DNS::Resource::IN::MX).empty?
record.errors[attribute] << (options[:message] || "does not have a valid domain")
end
rescue Resolv::ResolvError, Resolv::ResolvTimeout
record.errors[attribute] << (options[:message] || "does not have a valid domain")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment