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 }

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

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

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:

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

Ah yes you're right about "foo@bar"@rails.info

-sunny
http://ezror.com

Felix Schäfer wrote:

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.

Hi,

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

http://github.com/spectator/validates_email

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,

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.

Michael Pavling 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,

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

Marnen Laibow-Koser wrote:

Greg Donald 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/ .

Best,

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

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.

Michael Pavling wrote: