Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best/easy way to validate an email address in Ruby?

What is the best/easy way to validate an email address in ruby (on the server side)?

like image 836
Ran Avatar asked Jan 23 '11 22:01

Ran


People also ask

What is the best way to validate email?

Double opt-in is the best way to validate email addresses. If your customers sign up for your email list or newsletter, send them an email that requests them to validate by responding. Many people don't like this option, as it may reduce your overall opt-in rates, but we believe it is better to have good data.

Should you validate email with regex?

Don't use regexes for validating emails, unless you have a good reason not to. Use a verification mail instead. In most cases, a regex that simply checks that the string contains an @ is enough.


1 Answers

You could look whether or not it matches a regexp like the one used in this Rails validator:

validates_format_of :email,:with => /\A[^@\s]+@([^@\s]+\.)+[^@\s]+\z/ 

But if you use Devise, simply do:

validates_format_of :email,:with => Devise::email_regexp 

Source: http://lindsaar.net/2008/4/14/tip-4-detecting-a-valid-email-address

Edit 1:

useful website for tests: http://www.rubular.com/

like image 125
apneadiving Avatar answered Sep 20 '22 23:09

apneadiving