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.

1

3 Answers 3

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.

1
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.

1

Try this:

1
  • this REGEX allows for emails like "test@example/x.com" and therefor "test@example/////////////////////////////////x.com" Apr 1, 2010 at 17:34

Your Answer

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