Created
August 10, 2012 13:33
-
-
Save jacaetevha/3314234 to your computer and use it in GitHub Desktop.
valid email and url
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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