0.0
No commit activity in last 3 years
No release in over 3 years
Validate email address regex
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

#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