Simple way to validate email+domain with Ruby on Rails

require ‘resolv’ #”gem install resolv-ipv6favor

email = “foo@example.com” #test with valid email
split_email = email.split(“@”) #split string with “@”
domain = split_email[1].to_s #get domain name

if email =~ (/\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i)
#checking domain name
Resolv::DNS.open do |dns|
@staus = dns.getresources(domain, Resolv::DNS::Resource::IN::MX)
end
else
errors_add(:email, ‘domain name can not be found.’)
end

Advertisements