Skip to content

Instantly share code, notes, and snippets.

@dinedal
Created January 12, 2014 20:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dinedal/d75a81bffdaabd6f6c72 to your computer and use it in GitHub Desktop.
Save dinedal/d75a81bffdaabd6f6c72 to your computer and use it in GitHub Desktop.
## Email validation
#
# Submitter: Paul Bergeron - paul.d.bergeron@gmail.com
# Website: pauldbergeron.com
# Github: https://github.com/dinedal
# Location: San Francisco - CA
#
# A few notes:
#
# Never use this in production. A real email validation method should actually
# make an attempt to resolve the hostname associated with email,
# as this is the only really 100% reliable way to ensure that a given email
# actually has a valid hostname.
#
# This does mean that `someone@com` would fail validation, but a real
# production service probably doesn't even want to attempt to send email to
# that address anyway.
class EmailValidator
def validate(email)
email = email.to_s.strip
regex = /^ # START
(".*" # username with quotes so special chars match
| # OR
[a-zA-Z0-9.+]*) # username no quotes
@ # @
[a-zA-Z0-9.?]* # hostname or TLD
$ # END
/x
if regex.match(email)
true
else
false
end
end
end
email_validator = EmailValidator.new
File.open(ARGV.first).readlines.each do |line|
next if line.strip.empty?
puts email_validator.validate(line)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment