Validate an email address with Resolv::DNS : Utility Regexps « Development « Ruby






Validate an email address with Resolv::DNS


require 'resolv'
def valid_email_host?(email)
  hostname = email[(email =~ /@/)+1..email.length]
  valid = true
  begin
    Resolv::DNS.new.getresource(hostname, Resolv::DNS::Resource::IN::MX)
  rescue Resolv::ResolvError
    valid = false
  end
  return valid
end

valid_email_host?('j@l.m')       # => false
valid_email_host?('joe@oreilly.com')          # => true

 








Related examples in the same category

1.Validate an email address
2.Checking a Credit Card Checksum