Skip to content

Instantly share code, notes, and snippets.

@jacaetevha
Created August 10, 2012 13:33
Show Gist options
  • Select an option

  • Save jacaetevha/3314234 to your computer and use it in GitHub Desktop.

Select an option

Save jacaetevha/3314234 to your computer and use it in GitHub Desktop.
valid email and url
# gratuitously lifted from Vito Botta at http://vitobotta.com/sinatra-contact-form-jekyll/
require 'resolv'
def valid_email?(email)
if email =~ /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/
domain = email.match(/\@(.+)/)[1]
Resolv::DNS.open do |dns|
@mx = dns.getresources(domain, Resolv::DNS::Resource::IN::MX)
end
@mx.size > 0 ? true : false
else
false
end
end
def valid_url? url
return true if url == "http://"
!(url =~ /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix).nil?
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment