rails3 validate email format

308 views
Skip to first unread message

Nadal

unread,
Sep 26, 2010, 12:54:36 PM9/26/10
to Ruby on Rails: Talk
I am working on a rails3 project. Is there a tool to validate the
format of email address. It does not have to be fancy regex. Just a
few simple validations. However I don't want to reinvent the wheel?

Sampath Munasinghe

unread,
Sep 27, 2010, 2:47:52 AM9/27/10
to rubyonra...@googlegroups.com
you can use javascript or your own regx. do you need the code?


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.


Ugis Ozols

unread,
Sep 27, 2010, 5:01:11 AM9/27/10
to Ruby on Rails: Talk
I'm using this:

:format => { :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i }

Sunny Ezror

unread,
Sep 27, 2010, 5:19:10 AM9/27/10
to rubyonra...@googlegroups.com
I use whatever that comes with restful_authentication scaffold generator
and for the most part it works fine for me:
/^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i

If you want to learn more about this or more control over it, I suggest
you read http://www.regular-expressions.info/email.html

-sunny
http://ezror.com
--
Posted via http://www.ruby-forum.com/.

Felix Schäfer

unread,
Sep 27, 2010, 5:51:41 AM9/27/10
to rubyonra...@googlegroups.com

Using a regex to validate an email format is in most cases a bad idea, the 2 proposed regexes won't for example recognize quoted local parts, which are valid (e.g. "foo@bar"@rails.info is a valid address). There's a validate_email_format_of plugin on github[1] which works fine on rails2, I think there is a branch/fork for rails3 somewhere too. Another method would be to just drop an EmailValidator into your lib so that you can reuse it at other places in your rails app, have a look at [2] (yes, it still is a regex and only matches what the RFC refers to as "addr-spec" as opposed to a "full" address, but a not-so-bad one).

Anyway, I'm a little surprised that rails3 having gone from TMail to Mail hasn't added a validator that would re-use the one already present in Mail…

Regards,

Felix


[1] http://github.com/alexdunae/validates_email_format_of
[2] http://lindsaar.net/2010/1/31/validates_rails_3_awesome_is_true

Sunny Ezror

unread,
Sep 27, 2010, 6:57:19 AM9/27/10
to rubyonra...@googlegroups.com
Felix, I believe it does. In fact I did a quick check of both .info and
.me and they worked fine.

-sunny
http://ezror.com

--
Posted via http://www.ruby-forum.com/.

Felix Schäfer

unread,
Sep 27, 2010, 7:07:39 AM9/27/10
to rubyonra...@googlegroups.com

Am 27.09.2010 um 12:57 schrieb Sunny Ezror:

> Felix, I believe it does. In fact I did a quick check of both .info and
> .me and they worked fine.

The point being the localpart "foo@bar" (and yes, the quotes are part of the localpart) in "foo@bar"@rails.info, not the TLD.

Oh, and you should use \A and \z (string delimiters) over ^ and $ (line delimiters), as your regex will also validate strings of the form: f...@bar.com\nSome-funky-DB-buster .

Best,

Felix

Sunny Ezror

unread,
Sep 27, 2010, 7:13:00 AM9/27/10
to rubyonra...@googlegroups.com
Ah yes you're right about "foo@bar"@rails.info

-sunny
http://ezror.com

--
Posted via http://www.ruby-forum.com/.

radhames brito

unread,
Sep 27, 2010, 11:05:49 AM9/27/10
to rubyonra...@googlegroups.com

there is a plugin/gem that tries to find what ever its you have after the @ in the dns servers mx records, i have never use it.

petr.blaho

unread,
Sep 28, 2010, 10:20:23 AM9/28/10
to Ruby on Rails: Talk
Hi,

I think this is good plugin for email validation...

http://github.com/spectator/validates_email

PB

Marnen Laibow-Koser

unread,
Sep 28, 2010, 11:49:46 AM9/28/10
to rubyonra...@googlegroups.com

No reasonable regex will do the trick, since e-mail addresses can take
so many formats. Just check for an @ and one . , and trust the user for
the rest. If you need to check whether the address works, send the user
an activation code.

Do not use regexes to validate e-mail addresses.

Best,
-- 
Marnen Laibow-Koser
http://www.marnen.org
mar...@marnen.org

Greg Donald

unread,
Sep 28, 2010, 12:29:50 PM9/28/10
to rubyonra...@googlegroups.com
On Tue, Sep 28, 2010 at 10:49 AM, Marnen Laibow-Koser
<li...@ruby-forum.com> wrote:
> Do not use regexes to validate e-mail addresses.

Pffft.


--
Greg Donald
destiney.com | gregdonald.com

Michael Pavling

unread,
Sep 28, 2010, 12:51:49 PM9/28/10
to rubyonra...@googlegroups.com
On 28 September 2010 17:29, Greg Donald <gdo...@gmail.com> wrote:
> On Tue, Sep 28, 2010 at 10:49 AM, Marnen Laibow-Koser
> <li...@ruby-forum.com> wrote:
>> Do not use regexes to validate e-mail addresses.
>
> Pffft.

Nah, I'm with Marnen on this. Trying to regex validate an email with
100% surety is a sure-fire way of annoying someone. Same with
postcodes, and even more so with names (can you believe that some
people think they can regex what *name* a person may have - I've seen
it; it's rubbish! :-)

You can probably regex close to all potential email addresses, but as
Marnen says, the best way to validate an email address is to send an
email to it.

Marnen Laibow-Koser

unread,
Sep 28, 2010, 1:39:37 PM9/28/10
to rubyonra...@googlegroups.com
Michael Pavling wrote:
> On 28 September 2010 17:29, Greg Donald <gdo...@gmail.com> wrote:
>> On Tue, Sep 28, 2010 at 10:49 AM, Marnen Laibow-Koser
>> <li...@ruby-forum.com> wrote:
>>> Do not use regexes to validate e-mail addresses.
>>
>> Pffft.
>
> Nah, I'm with Marnen on this. Trying to regex validate an email with
> 100% surety is a sure-fire way of annoying someone.

I have seen regexes that purport to actually be correct for e-mail
validation. They are on the order of a page in length when printed in
reasonably-sized type.

> Same with
> postcodes,

Really? Shouldn't you at least be able to come up with
*country-specific* regexes for those?

> and even more so with names (can you believe that some
> people think they can regex what *name* a person may have - I've seen
> it; it's rubbish! :-)

As the proud bearer of a hyphenated last name, I am intimately familiar
with this brand of rubbish.

>
> You can probably regex close to all potential email addresses, but as
> Marnen says, the best way to validate an email address is to send an
> email to it.

Best,

Greg Donald

unread,
Sep 28, 2010, 1:41:07 PM9/28/10
to rubyonra...@googlegroups.com
On Tue, Sep 28, 2010 at 11:51 AM, Michael Pavling <pav...@gmail.com> wrote:
>>> Do not use regexes to validate e-mail addresses.
>>
>> Pffft.
>
> Nah, I'm with Marnen on this.

You might get away without validating email addresses for a small
site, one where you can easily bulk-delete bounced messages. For a
large site you're creating a ton of extra work for someone, all
because you can't be bothered to write a simple regex.

I couldn't imagine paying a marketing company for new users and not at
least doing a simple regex validation on the email address as they
arrive. You guys are really in-experienced.

Marnen Laibow-Koser

unread,
Sep 28, 2010, 1:58:32 PM9/28/10
to rubyonra...@googlegroups.com
Marnen Laibow-Koser wrote:

> Greg Donald wrote:
>> On Tue, Sep 28, 2010 at 11:51 AM, Michael Pavling <pav...@gmail.com>
>> wrote:
>>>>> Do not use regexes to validate e-mail addresses.
>>>>
>>>> Pffft.
>>>
>>> Nah, I'm with Marnen on this.
>>
>> You might get away without validating email addresses for a small
>> site, one where you can easily bulk-delete bounced messages. For a
>> large site you're creating a ton of extra work for someone, all
>> because you can't be bothered to write a simple regex.
>
> A "simple" regex won't do it. The range of legitimate e-mail address
> forms is simply too great. Go read RFC 2822 and
> http://www.linuxjournal.com/article/9585&ved==AFQjCNHU_zypiBJyMSvi-aD2ueMftTOecg

Also RFC 5322 and the (just published, apparently)
http://www.dominicsayers.com/isemail/ .

Marnen Laibow-Koser

unread,
Sep 28, 2010, 1:50:38 PM9/28/10
to rubyonra...@googlegroups.com
Greg Donald wrote:
> On Tue, Sep 28, 2010 at 11:51 AM, Michael Pavling <pav...@gmail.com>
> wrote:
>>>> Do not use regexes to validate e-mail addresses.
>>>
>>> Pffft.
>>
>> Nah, I'm with Marnen on this.
>
> You might get away without validating email addresses for a small
> site, one where you can easily bulk-delete bounced messages. For a
> large site you're creating a ton of extra work for someone, all
> because you can't be bothered to write a simple regex.

A "simple" regex won't do it. The range of legitimate e-mail address

forms is simply too great. Go read RFC 2822 and

http://www.linuxjournal.com/article/9585&ved=0CBYQFjAC&usg=AFQjCNHU_zypiBJyMSvi-aD2ueMftTOecg

> I couldn't imagine paying a marketing company for new users and not at
> least doing a simple regex validation on the email address as they
> arrive. You guys are really in-experienced.

I'm experienced enough to know that e-mail addresses are a lot more
complex than inexperienced people think. Go educate yourself with the
references above.

>
>
> --
> Greg Donald
> destiney.com | gregdonald.com

Best,

Michael Pavling

unread,
Sep 28, 2010, 2:36:02 PM9/28/10
to rubyonra...@googlegroups.com
On 28 September 2010 18:41, Greg Donald <gdo...@gmail.com> wrote:
> You guys are really in-experienced.

Nice tone. I didn't know you'd evaluated my CV (it's not exactly
secret materiel though)

> I couldn't imagine paying a marketing company for new users and not at
> least doing a simple regex validation on the email address as they
> arrive.

The "simple regex" you'd use for such a check is that if a string has
some characters followed by an "@" then some more characters with at
least one full-stop in them ("period", for the leftpondians), then
it's *possibly* an email address, which could be either valid (by
being of a format that passes the relevant RFC) and valid (there is
actually a mailbox at the other end of it). Marnen has already said
that such a cursory check is where you should really stop unless you
want a hiding to nothing (and without reverse-engineering them to
check for sure, it looks like the example regexes above do just this).
However, you can't know *for sure* whether a string passes the former
meaning of "valid" without a *really big* regex, and you can't know
the latter unless you send a message to it.

> You might get away without validating email addresses for a small
> site, one where you can easily bulk-delete bounced messages.  For a
> large site you're creating a ton of extra work for someone, all
> because you can't be bothered to write a simple regex.

What work? For who? I'm curious, because I think we must be talking at
crossed-purposes if you think we're so far out of line.

If you want to "validate" an email address that someone has typed in
to a "please enter your email address for registration" box, the
simplest way to validate that address is to send it an email (after
performing the "simple" check to see if it even vaguely resembles an
email address), with a unique link for the recipient to click to prove
the email address works, and that they have access to it.

If you've bought 10,000,000 "email addresses" from a marketing
company, then of course a "simple" regex check against each is
sensible; what we've said is "bad" is the hope that one can try to
guarantee a string could be a valid email address (this, some people
do try to do).

Michael Pavling

unread,
Sep 28, 2010, 2:40:56 PM9/28/10
to rubyonra...@googlegroups.com
On 28 September 2010 18:39, Marnen Laibow-Koser <li...@ruby-forum.com> wrote:
>> Same with
>> postcodes,
>
> Really?  Shouldn't you at least be able to come up with
> *country-specific* regexes for those?

I'm being UK-centric here, so YMMV, but I once worked at a company
which had a postcode ("WC1E 4HR") that I regularly got "sorry, that
postcode is invalid" messages popped-up at me when I tried to buy
stuff online. That was mostly due to "simple" regexes not accepting a
letter after a number in the district code.

Marnen Laibow-Koser

unread,
Sep 28, 2010, 3:13:35 PM9/28/10
to rubyonra...@googlegroups.com

Interesting. UK postcodes are certainly less regular in format than
those of most other countries (U.S.: /^\d{5}(-?\d{4})?$/; Canada:
/^[a-z]\d[a-z]\s*\d[a-z]\d$/i ; Netherlands: /^\d{4}\s*[a-z]{2}$/i ...)

Hery Hallelujah

unread,
Dec 15, 2010, 6:33:26 PM12/15/10
to rubyonra...@googlegroups.com
Felix Schäfer wrote in post #944087:

> Anyway, I'm a little surprised that rails3 having gone from TMail to
> Mail hasn't added a validator that would re-use the one already present
> in Mail…
>
> Regards,
>
> Felix
>
>
> [1] http://github.com/alexdunae/validates_email_format_of
> [2] http://lindsaar.net/2010/1/31/validates_rails_3_awesome_is_true

You can check out my email validation with Mail :

http://my.rails-royce.org/2010/07/21/email-validation-in-ruby-on-rails-without-regexp/

Regards,

Hery

Reply all
Reply to author
Forward
0 new messages