Carsten Nielsen has created an amazing plugin for rails called email veracity. It takes all of the heavy lifting out of what can often be a problematic part of data validation, and takes it a step further than most.

Not only does email veracity remove the need for a convoluted regexp to match against the email string, but it will also check the domain records to ensure that the MX entry is currently accepting mail, wow!

So how do I check the format of an email address?

Add this to the code on your model definition:

validates_email_veracity_of :email

that single line does the domain check too! You can turn it off, apply a timeout, and/or a custom error message if you like too:

validates_email_veracity_of :email, :domain_check => false, :timeout => 5, :message => "is not valid"


It's available as a plugin so use piston, or the following command:

script/plugin install