» Ruby »
Email address validation using RFC 822


Email address regular expression to validate an email address using RFC 822.

The original PHP code is by Cal Henderson, see

Translated to Ruby by Tim Fletcher, with changes suggested by Dan Kubb, see

For docs go to

Want to help? We're happy to get pull requests.

Related links:

Install quickstart


gem install sixarm_ruby_email_address_validation


gem "sixarm_ruby_email_address_validation", ">=2.0.1", "<2"


require "sixarm_ruby_email_address_validation"

Install with security (optional)

To enable high security for all our gems:

gem cert --add sixarm.pem
gem sources --add

To install with high security:

gem install sixarm_ruby_email_address_validation --trust-policy HighSecurity


if EmailAddressValidation::Pattern=~""

Pattern Match

To find an email address in a string, do the pattern match then use the result, which is the match's string position:

Example of match position:

=> 0

EmailAddressValidation::Pattern=~'... ...'
=> 4

EmailAddressValidation::Pattern=~'... hello world ...'
=> nil

Pattern Match Exact

To do a pattern match to the entire string, use the EmailAddressValidation::PatternExact.

The entire string must be one email address.

Example of pattern match:

if EmailAddressValidation::PatternExact=~''
#=> truthy

if EmailAddressValidation::PatternExact=~'... ...'
#=> falsey

This pattern is especialy useful to validate an email address.

Example to validate an email address:

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

Rails Validation (optional)

To add email address validation to a typical Ruby On Rails model:

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

Frequently Asked Questions

Q. Does this handle unusual email addresses, like

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.