SixArm / sixarm_ruby_email_address_validation

SixArm.com » Ruby » Email address validation using RFC 822
http://sixarm.com
Other
10 stars 0 forks source link
address email gem rfc rfc-822 ruby validation

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

Gem Version Build Status Code Climate

w

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

Gem

To install this gem in your shell or terminal:

gem install sixarm_ruby_email_address_validation

Gemfile

To add this gem to your Gemfile:

gem 'sixarm_ruby_email_address_validation'

Require

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.

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.

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.