https://github.com/substancelab/activemodel-email_address_validator
Opinionated email address validation for ActiveModels/ActiveRecord
https://github.com/substancelab/activemodel-email_address_validator
activemodel email validation
Last synced: 8 months ago
JSON representation
Opinionated email address validation for ActiveModels/ActiveRecord
- Host: GitHub
- URL: https://github.com/substancelab/activemodel-email_address_validator
- Owner: substancelab
- License: mit
- Created: 2015-03-08T20:24:05.000Z (almost 11 years ago)
- Default Branch: main
- Last Pushed: 2024-11-11T04:22:05.000Z (over 1 year ago)
- Last Synced: 2024-11-14T13:11:54.414Z (over 1 year ago)
- Topics: activemodel, email, validation
- Language: Ruby
- Size: 78.1 KB
- Stars: 1
- Watchers: 5
- Forks: 2
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Email Address Validator
## ActiveModel-style email address format validator
[](https://codeclimate.com/github/substancelab/activemodel-email_address_validator) [](https://coveralls.io/github/substancelab/activemodel-email_address_validator?branch=more-badges)
Whenever I have wanted to validate an email address it has been because I wanted to be somewhat certain I can send an email to someone. Usually this happens as part of a signup procedure.
At this point I have pretty much one criteria:
* Don't reject a valid email address realistically in use by a potential user. Err on the side of accepting too much.
Quite frankly, I don't care about the RFC at this point, neither does the user. I care that my users can enter their email address and get on with using my product. I appreciate it if the application catches any misspellings of their email addresses, though - this is the opportune moment for them to correct it.
## Requirements
* Should not accept local email addresses: No user ever needed to sign up using `"postmaster@localhost"` or `"bob@1.2.3.4"` even though they are perfectly valid email addresses.
* Must work with I18n like Rails' built-in validators do. If not configured otherwise, the default translation key must be `:invalid`.
## Usage examples
### Simplest case
validates :email, :email_address => true
### Bring your own logic
If the default behavior isn't enough for you, you can include a custom rule for email addresses. For example to match the email addresses against a regular expression:
validates :email, :email_address => {:with => /.+@.+\..+/}
Or you could go beyound simple matching and validate that all email adresses belong to the same company:
validates :email, :email_address => {
:with => proc { |address| address.end_with?("@substancelab.com") }
}
You can even match against multiple rules, in which case all rules must pass:
validates :email, :email_address => {
:with => [
proc { |address| address.match(/.+@.+\..+/) },
proc { |address| !address.end_with?("@outlook.com") },
]
}
Do note that supplying your own rules means that the default email address validation isn't run - you're on your own, basically.
### Verify domain (still to be done - pull request, anybody?)
This also checks that the domain actually has an MX record. Note this might take a while because of DNS lookups.
validates :email, :email_address => {:mx => true}
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'activemodel-email_address_validator'
```
And then execute:
$ bundle
Or install it yourself as:
$ gem install activemodel-email_address_validator
## Resources
* http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address
* http://davidcel.is/blog/2012/09/06/stop-validating-email-addresses-with-regex/
* http://blog.myitcv.org.uk/2013/03/14/validators-in-rails-using-email-as-an-example.html
## Other libraries
### Serverside
* https://github.com/balexand/email_validator
* https://github.com/codyrobbins/active-model-email-validator
* https://github.com/franckverrot/activevalidators
* https://github.com/hallelujah/valid_email
* https://github.com/micke/valid_email2
* https://github.com/validates-email-format-of/validates_email_format_of
### Clientside
* https://github.com/mailcheck/mailcheck
## Contributing
1. Fork it ( https://github.com/substancelab/activemodel-email_address_validator/fork )
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request