Module: ValidatesEmailFormatOf
- Defined in:
- lib/validates_email_format_of.rb
Constant Summary collapse
- LocalPartSpecialChars =
Regexp.escape('!#$%&\'*-/=?+-^_`{|}~')
- LocalPartUnquoted =
'(([[:alnum:]' + LocalPartSpecialChars + ']+[\.\+]+))*[[:alnum:]' + LocalPartSpecialChars + '+]+'
- LocalPartQuoted =
'\"(([[:alnum:]' + LocalPartSpecialChars + '\.\+]*|(\\\\[\x00-\xFF]))*)\"'
- Regex =
Regexp.new('^((' + LocalPartUnquoted + ')|(' + LocalPartQuoted + ')+)@(((\w+\-+)|(\w+\.))*\w{1,63}\.[a-z]{2,6}$)', Regexp::EXTENDED | Regexp::IGNORECASE)
Class Method Summary collapse
- .validate_email_domain(email) ⇒ Object
-
.validate_email_format(email, options = {}) ⇒ Object
Validates whether the specified value is a valid email address.
Class Method Details
.validate_email_domain(email) ⇒ Object
9 10 11 12 13 14 15 |
# File 'lib/validates_email_format_of.rb', line 9 def self.validate_email_domain(email) domain = email.match(/\@(.+)/)[1] Resolv::DNS.open do |dns| @mx = dns.getresources(domain, Resolv::DNS::Resource::IN::MX) end @mx.size > 0 ? true : false end |
.validate_email_format(email, options = {}) ⇒ Object
Validates whether the specified value is a valid email address. Returns nil if the value is valid, otherwise returns an array containing one or more validation error messages.
Configuration options:
-
message- A custom error message (default is: “ does not appear to be a valid e-mail address”) -
check_mx- Check for MX records (default is false) -
mx_message- A custom error message when an MX record validation fails (default is: “ is not routable.”) -
withThe regex to use for validating the format of the email address (default is ValidatesEmailFormatOf::Regex)</tt>
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/validates_email_format_of.rb', line 25 def self.validate_email_format(email, ={}) = { :message => ' does not appear to be a valid e-mail address', :check_mx => false, :mx_message => ' is not routable.', :with => ValidatesEmailFormatOf::Regex } .merge!() {|key, old, new| old} # merge the default options into the specified options, retaining all specified options # local part max is 64 chars, domain part max is 255 chars # TODO: should this decode escaped entities before counting? begin domain, local = email.reverse.split('@', 2) rescue return [ [:message] ] end unless email =~ [:with] and not email =~ /\.\./ and domain.length <= 255 and local.length <= 64 return [ [:message] ] end if [:check_mx] and !ValidatesEmailFormatOf::validate_email_domain(email) return [ [:mx_message] ] end return nil # represents no validation errors end |