Rails3 validate email format

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?

you can use javascript or your own regx. do you need the code?

I’m using this:

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

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] GitHub - validates-email-format-of/validates_email_format_of: Validate e-mail addreses against RFC 2822 and RFC 3696 with this Ruby on Rails plugin and gem.
[2] Redirecting…

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] GitHub - validates-email-format-of/validates_email_format_of: Validate e-mail addreses against RFC 2822 and RFC 3696 with this Ruby on Rails plugin and gem.
[2] Redirecting…

Am 27.09.2010 um 12:57 schrieb Sunny E.:

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:
[email protected]\nSome-funky-DB-buster .

Best,

Felix

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 How to Find or Validate an Email Address

-sunny
http://ezror.com

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 E.:

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:
[email protected]\nSome-funky-DB-buster .

Best,

Felix

Hi,

I think this is good plugin for email validation…

PB

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
[email protected]

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.

On Tue, Sep 28, 2010 at 10:49 AM, Marnen Laibow-Koser
[email protected] wrote:

Do not use regexes to validate e-mail addresses.

Pffft.


Greg D.
destiney.com | gregdonald.com

On 28 September 2010 17:29, Greg D. [email protected] wrote:

On Tue, Sep 28, 2010 at 10:49 AM, Marnen Laibow-Koser
[email protected] 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! :slight_smile:

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.

On Tue, Sep 28, 2010 at 11:51 AM, Michael P. [email protected]
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 D.
destiney.com | gregdonald.com

Greg D. wrote:

On Tue, Sep 28, 2010 at 11:51 AM, Michael P. [email protected]
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 D.
destiney.com | gregdonald.com

Best,
–Â
Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Michael P. wrote:

On 28 September 2010 17:29, Greg D. [email protected] wrote:

On Tue, Sep 28, 2010 at 10:49 AM, Marnen Laibow-Koser
[email protected] 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! :slight_smile:

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
[email protected]

Marnen Laibow-Koser wrote:

Greg D. wrote:

On Tue, Sep 28, 2010 at 11:51 AM, Michael P. [email protected]
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)
is_email by dominicsayers .

Best,
–Â
Marnen Laibow-Koser
http://www.marnen.org
[email protected]

On 28 September 2010 18:41, Greg D. [email protected] 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 P. wrote:

On 28 September 2010 18:39, Marnen Laibow-Koser [email protected]
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
[email protected]

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] GitHub - validates-email-format-of/validates_email_format_of: Validate e-mail addreses against RFC 2822 and RFC 3696 with this Ruby on Rails plugin and gem.
[2] Redirecting…

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