Email regex gem
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci
lib
test
.gitignore
LICENSE.txt
README.md
Rakefile
email_regex.gemspec

README.md

#email_regex

Author: Doug Wiegley

##Description

Provides a valid email regex that conforms to most valid RFC edges cases (disallows backticks), and allows for a few illegal patterns that are in common use.

##Installation

###For a rails application

Add the gem to your gemfile.

gem 'email_regex'

Then bundle install.

$ bundle install

###Install the gem on its own

$ gem install email_regex

##Usage

The regex is then accessible with the following:

EmailRegex::EMAIL_ADDRESS_REGEX

An example:

%w(invalid_email valid_email@example.com).select { |e| e if e =~ EmailRegex::EMAIL_ADDRESS_REGEX }
=> [valid_email@example.com]

###With ActiveRecord

This can be used with an ActiveRecord validation in two ways:

####With validates_format_of

class User < ActiveRecord::Base
  attr_accessible :email
  validates_format_of :email, with: EmailRegex::EMAIL_ADDRESS_REGEX
end

####With a custom validator

class User < ActiveRecord::Base
  attr_accessible :email
  validates :email, presence: true, email_format: true
end

Then, in lib/validators/email_format_validator.rb:

class EmailFormatValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    unless value =~ EmailRegex::EMAIL_ADDRESS_REGEX
      record.errors[attribute] << (options[:message] || "is invalid")
    end
  end
end

##Copyright

See LICENSE.txt for more details