Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
SixArm.com » Ruby » Email address validation using RFC 822
Ruby
Branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
.yardoc
coverage
doc
lib
test
.codeclimate.yml
.document
.gemtest
.gitattributes
.gitignore
.rspec
.travis.yml
CHANGES.md
CONTRIBUTING.md
Gemfile
Gemfile.lock
LICENSE.md
README.md
Rakefile
VERSION
sixarm_ruby_email_address_validation-2.0.2-sha512.txt
sixarm_ruby_email_address_validation-2.0.2.gem
sixarm_ruby_email_address_validation.gemspec

README.md

SixArm.com » Ruby »
Email address validation using RFC 822

Gem Version Build Status Code Climate Coverage Status

Introduction

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:

Install

To install using a Gemfile, add this:

gem "sixarm_ruby_email_address_validation", ">= 2.0.2", "< 3"

To install using the command line, run this:

gem install sixarm_ruby_email_address_validation -v ">= 2.0.2, < 3"

To install using the command line with high security, run this:

wget http://sixarm.com/sixarm.pem
gem cert --add sixarm.pem && gem sources --add http://sixarm.com
gem install sixarm_ruby_email_address_validation -v ">= 2.0.2, < 3" --trust-policy HighSecurity

To require the gem in your code:

require "sixarm_ruby_email_address_validation"

Pattern

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 =~ "alice@example.com"
=> 0

EmailAddressValidation::Pattern =~ "--- alice@example.com ---"
=> 4

Pattern Exact

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 =~ "alice@example.com'
#=> 0

EmailAddressValidation::PatternExact =~ "--- alice@example.com ---"
#=> nil

Examples

The patterns are easy to use in your own code.

Example of a condition:

text = "alice@example.com"
if EmailAddressValidation::PatternExact =~ text
  puts "valid"
else
  puts "invalid"
end

Example of a method:

def valid?(text)
  EmailAddressValidation::PatternExact =~ text ? true : false
end

valid?("alice@example.com") #=> true
valid?("alice") #=> false

Example of a scan which gets all the email adddresses:

text = "To alice@example.com and bob@example.org and others"
text.scan(EmailAddressValidation::Pattern)
#=> ["alice@example.com", "bob@example.org"]

Example of a scan which iterates on each email address:

text = "To alice@example.com and bob@example.org and others"
text.scan(EmailAddressValidation::Pattern) do |match|
  puts match
end
#=>
alice@example.com
bob@example.org

Example of a Rails user class:

class User
  include EmailAddressValidation
  validates :email_address, :format => { :with => EmailAddressValidation::PatternExact }
end

Frequently Asked Questions

Q. Does this handle unusual email addresses, like foo+bar@my.com?

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.

Something went wrong with that request. Please try again.