Skip to content

codyrobbins/active-model-email-validator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Active Model Email Validator

This is a simple ActiveModel validator for email addresses built on top of the Mail gem. Instead of trying to devise an overly complex custom (and probably incorrect) regular expression to validate email addresses that is compliant with RFC 5321/5322—a difficult task—it instead relies on the Mail gem to parse the address. Since the Mail gem is an actively maintained library for working with email, if it can’t deal with an address it’s probably not worth attempting to send to anyways.

An additional check is performed to ensure that the domain name in the address has at least two components—that is, a top-level domain and one subdomain. The validator purposefully errs on the side of inclusivity rather than exclusivity: it might allow some invalid email addresses, but it hopefully doesn’t disallow valid addresses.

Full documentation is at RubyDoc.info.

Example

The following model uses ActiveModel::Validations::PresenceValidator and ActiveRecord::Validations::UniquenessValidator to ensure the presence and uniqueness of the user’s email attribute. The third line uses EmailValidator to check that the email address is valid.

class User < ActiveRecord::Base
  validates(:email,
            :presence   => {:message => "Email can't be blank"},
            :uniqueness => {:message => 'Email must be unique'},
            :email      => {:message => 'Email must be valid'})
end

Note

Currently there isn’t a validates_email class-level helper method because I’ve never had the need for it in practice, but I may add one at some point.

Colophon

See also

If you like this gem, you may also want to check out Static Model, HTTP Error, or Email Test Helpers.

Tested with

  • ActiveModel 3.0.5 — 20 May 2011

Contributing

To send patches, please fork on GitHub and submit a pull request.

Credits

© 2011 Cody Robbins. See LICENSE for details.