7

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.

  • possible duplicate of [How to use a regular expression to validate an email addresses?](http://stackoverflow.com/questions/201323/how-to-use-a-regular-expression-to-validate-an-email-addresses) – Marc-André Lafortune May 17 '12 at 15:22

3 Answers3

12

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, i.e. returns false for an empty address and also checks that the address contains a domain.

Intrepidd
  • 19,772
  • 6
  • 55
  • 63
Wolfgang
  • 4,865
  • 2
  • 29
  • 29
2

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.

womble
  • 12,033
  • 5
  • 52
  • 66
1

Try this:

Lonzo
  • 2,758
  • 4
  • 22
  • 27