- Git: https://github.com/SixArm/sixarm_ruby_email_address_validation
- Doc: http://sixarm.com/sixarm_ruby_email_address_validation/doc
- Gem: https://rubygems.org/gems/sixarm_ruby_email_address_validation
- Contact: Joel Parker Henderson, [email protected]
- Project: changes, license, contributing.
w
Email address regular expression to validate an email address using RFC 822.
The original PHP code is by Cal Henderson, see http://iamcal.com/publish/articles/php/parsing_email/
Translated to Ruby by Tim Fletcher, with changes suggested by Dan Kubb, see http://tfletcher.com/lib/rfc822.rb
For docs go to http://sixarm.com/sixarm_ruby_email_address_validation/doc
Want to help? We're happy to get pull requests.
Related links:
- RFC 2822: http://tools.ietf.org/html/rfc2822
- Dominic Sayers is_email: http://isemail.info/
- Wikipedia email address: http://en.wikipedia.org/wiki/Email_address
- Email address validator with parsing expression grammar: https://github.com/larb/email_address_validator
- ActiveModel gem to delegate to Mail gem: https://github.com/codyrobbins/active-model-email-validator
To install this gem in your shell or terminal:
gem install sixarm_ruby_email_address_validation
To add this gem to your Gemfile:
gem 'sixarm_ruby_email_address_validation'
To require the gem in your code:
require 'sixarm_ruby_email_address_validation'
To find an email address, anywhere in a string, use the Pattern
constant.
- If a match is anywhere in the string, then return the character position index.
- If there's no match, then return nil.
Example:
EmailAddressValidation::Pattern =~ "[email protected]"
=> 0
EmailAddressValidation::Pattern =~ "--- [email protected] ---"
=> 4
To find an email address, and ensure that it is the entire string, use the PatternExact
constant.
- If a match is the entire string, then return the character position index, which is always 0.
- Otherwise return nil.
Example:
EmailAddressValidation::PatternExact =~ "[email protected]'
#=> 0
EmailAddressValidation::PatternExact =~ "--- [email protected] ---"
#=> nil
The patterns are easy to use in your own code.
Example of a condition:
text = "[email protected]"
if EmailAddressValidation::PatternExact =~ text
puts "valid"
else
puts "invalid"
end
Example of a method:
def valid?(text)
EmailAddressValidation::PatternExact =~ text ? true : false
end
valid?("[email protected]") #=> true
valid?("alice") #=> false
Example of a scan which gets all the email adddresses:
text = "To [email protected] and [email protected] and others"
text.scan(EmailAddressValidation::Pattern)
#=> ["[email protected]", "[email protected]"]
Example of a scan which iterates on each email address:
text = "To [email protected] and [email protected] and others"
text.scan(EmailAddressValidation::Pattern) do |match|
puts match
end
#=>
[email protected]
[email protected]
Example of a Rails user class:
class User
include EmailAddressValidation
validates :email_address, :format => { :with => EmailAddressValidation::PatternExact }
end
Q. Does this handle unusual email addresses, like [email protected]?
A. Yes. It handles all RFC email addresses. If you find an RFC email address that fails, please let us know.
Q. Why use this in a Rails app?
A. We use it to detect a potential typo during user registration, so we can prompt the user to correct it before we send a typical welcome email with a registration link. If we didn't validate the email address format, then we would have sent the welcome email to the wrong user, or into the void.
Q. Why use this to get more than one email address?
A. We use it to find typos in large databases of email addresses where it's not a business option to send the user an email. For example, proofing an existing report of 10 million users in a CSV file. We need to contact anyone with a malformed email address, so we actually call them if we have their phone number. We can't send these people an email, because the address is invalid.