Forum: Ruby on Rails rails3 validate email format

Posted by Nadal (Guest)
on 2010-09-26 18:57
(Received via mailing list)
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?
Posted by Sampath Munasinghe (Guest)
on 2010-09-27 08:49
(Received via mailing list)
you can use javascript or your own regx. do you need the code?
Posted by Ugis Ozols (Guest)
on 2010-09-27 11:03
(Received via mailing list)
I'm using this:

:format => { :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i }
Posted by Senthil Nambi (senthilnambi)
on 2010-09-27 11:19
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 by Felix Schäfer (Guest)
on 2010-09-27 11:52
(Received via mailing list)
Am 26.09.2010 um 18:54 schrieb Nadal:

> 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?

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_aw...
Posted by Senthil Nambi (senthilnambi)
on 2010-09-27 12:57
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

Felix Schäfer wrote:
> Am 26.09.2010 um 18:54 schrieb Nadal:
> 
>> 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?
> 
> 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_aw...
Posted by Felix Schäfer (Guest)
on 2010-09-27 13:09
(Received via mailing list)
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: 
foo@bar.com\nSome-funky-DB-buster .

Best,

Felix
Posted by Senthil Nambi (senthilnambi)
on 2010-09-27 13:13
Ah yes you're right about "foo@bar"@rails.info

-sunny
http://ezror.com

Felix Schäfer wrote:
> 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: 
> foo@bar.com\nSome-funky-DB-buster .
> 
> Best,
> 
> Felix
Posted by radhames brito (Guest)
on 2010-09-27 17:13
(Received via mailing list)
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.
Posted by petr.blaho (Guest)
on 2010-09-28 16:21
(Received via mailing list)
Hi,

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

http://github.com/spectator/validates_email

PB
Posted by Marnen Laibow-Koser (marnen)
on 2010-09-28 17:49
Nadal wrote:
> 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?

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
marnen@marnen.org
Posted by Greg Donald (destiney)
on 2010-09-28 18:37
(Received via mailing list)
On Tue, Sep 28, 2010 at 10:49 AM, Marnen Laibow-Koser
<lists@ruby-forum.com> wrote:
> Do not use regexes to validate e-mail addresses.

Pffft.


--
Greg Donald
destiney.com | gregdonald.com
Posted by Michael Pavling (Guest)
on 2010-09-28 18:58
(Received via mailing list)
On 28 September 2010 17:29, Greg Donald <gdonald@gmail.com> wrote:
> On Tue, Sep 28, 2010 at 10:49 AM, Marnen Laibow-Koser
> <lists@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.
Posted by Marnen Laibow-Koser (marnen)
on 2010-09-28 19:39
Michael Pavling wrote:
> On 28 September 2010 17:29, Greg Donald <gdonald@gmail.com> wrote:
>> On Tue, Sep 28, 2010 at 10:49 AM, Marnen Laibow-Koser
>> <lists@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,
-- 
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
Posted by Greg Donald (destiney)
on 2010-09-28 19:42
(Received via mailing list)
On Tue, Sep 28, 2010 at 11:51 AM, Michael Pavling <pavling@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.


--
Greg Donald
destiney.com | gregdonald.com
Posted by Marnen Laibow-Koser (marnen)
on 2010-09-28 19:50
Greg Donald wrote:
> On Tue, Sep 28, 2010 at 11:51 AM, Michael Pavling <pavling@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=0CBYQ...

> 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,
-- 
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
Posted by Marnen Laibow-Koser (marnen)
on 2010-09-28 19:58
Marnen Laibow-Koser wrote:
> Greg Donald wrote:
>> On Tue, Sep 28, 2010 at 11:51 AM, Michael Pavling <pavling@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==AFQj...

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

Best,
-- 
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
Posted by Michael Pavling (Guest)
on 2010-09-28 20:38
(Received via mailing list)
On 28 September 2010 18:41, Greg Donald <gdonald@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).
Posted by Michael Pavling (Guest)
on 2010-09-28 20:43
(Received via mailing list)
On 28 September 2010 18:39, Marnen Laibow-Koser <lists@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.
Posted by Marnen Laibow-Koser (marnen)
on 2010-09-28 21:13
Michael Pavling wrote:
> On 28 September 2010 18:39, Marnen Laibow-Koser <lists@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.

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

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
Posted by Hery Hallelujah (hallelujah)
on 2010-12-16 00:33
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_aw...

You can check out my email validation with Mail :

http://my.rails-royce.org/2010/07/21/email-validat...

Regards,

Hery
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.