Ruby email check (RFC 2822)

Does anyone know what the regular expression in Ruby is to verify an email address is in proper RFC 2822 email format?

What I want to do is:

string.match(RFC_2822_REGEX)

where "RFC_2822_REGEX" is the regular expression to verify if my string is in valid RFC 2882 form.


You can use the mail gem to parse any string according to RFC2822 like so:

def valid_email( value )
  begin
   return false if value == ''
   parsed = Mail::Address.new( value )
   return parsed.address == value && parsed.local != parsed.address
  rescue Mail::Field::ParseError
    return false
  end
end

This checks if the email is provided, ie returns false for an empty address and also checks that the address contains a domain.


基于这个类似问题的答案,你可能想重新考虑使用正则表达式来实现这一点。


http://theshed.hezmatt.org/email-address-validator

Does regex validation based on RFC2822 rules (it's a monster of a regex, too), it can also check that the domain is valid in DNS (has MX or A records), and do a test delivery to validate that the MX for the domain will accept a message for the address given. These last two checks are optional.

链接地址: http://www.djcxy.com/p/92760.html

上一篇: 邮件字段使用正则表达式

下一篇: Ruby电子邮件检查(RFC 2822)