1
\$\begingroup\$

I wrote this short, plain Ruby lib to handle email address validation inside and outside of Rails applications, and I would like to know what you think.

require "i18n"
class EmailAddress
 VALID_EMAIL_ADDRESS_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
 def self.malformed?(email)
 email !~ VALID_EMAIL_ADDRESS_REGEX
 end
 def self.valid?(email)
 !malformed?(email)
 end
 attr_reader :email, :i18n_scope, :errors
 def initialize(email, i18n_scope: [:models, :email_address])
 @email = email
 @i18n_scope = i18n_scope
 @errors = []
 end
 def valid?
 validate
 errors.empty?
 end
 def validate
 message = I18n.t(:invalid, scope: i18n_scope)
 errors.push(message) if EmailAddress.malformed?(email)
 end
end

Usage can be as simple as:

if EmailAddress.valid?(params[:email))
 # do something
end

Or for remote validations, for example:

def email
 email = EmailAddress.new(params[:email])
 if email.valid?
 render nothing: true, status: 200
 else
 render json: { errors: email.errors }, status: 422
 end
end
200_success
145k22 gold badges190 silver badges478 bronze badges
asked Jan 29, 2015 at 14:46
\$\endgroup\$
2
  • 1
    \$\begingroup\$ The absolute most reliable way to test if an email is valid or not is to check the response of isemail.info/#{emailaddress}. Unfortunately it doesn't appear that he has a ruby gem for it, so if you have an internet connection then that should be your first choice. \$\endgroup\$ Commented Jan 29, 2015 at 17:45
  • \$\begingroup\$ I might write a gem for that then! \$\endgroup\$ Commented Jan 29, 2015 at 18:17

1 Answer 1

2
\$\begingroup\$

The Regex will validate some email addresses. Because the Domain Name System allows UniCode. the Regex does not match all valid characters in the Doman Name. Therefore it will not validate all valid email addresses.

Alternatives:

Validating over all allowable Unicode with Regex in a meaningful way is non-trivial.

  1. Sending a confirmation email requiring reply is a straight-forward method for validating email procedurally.

  2. When possible, not collecting email addresses at all is easy to implement.

answered Jan 29, 2015 at 16:27
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.