# Email validation

**URL:** <https://www.ruby-forum.com/t/email-validation/67363>\
**Category:** Rails\
**Created:** [September 2, 2006, 8:20pm UTC](https://www.ruby-forum.com/t/email-validation/67363 "2006-09-02T20:20:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![releod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9dc877/32.png) [@releod](https://www.ruby-forum.com/u/releod)\
**Post date:** [September 2, 2006, 8:20pm UTC](https://www.ruby-forum.com/t/email-validation/67363/1 "2006-09-02T20:20:55Z")

</div>

im back again…

ive been linked to [http://tfletcher.com/lib/rfc822.rb](http://tfletcher.com/lib/rfc822.rb) to use for e-mail  
validation, can someone explain how I use it with a validation  
statement?

right now i have validates\_format\_of :email, :with =\>

but im not sure how to incorporate it properly…

thanks again!

---

<div class="post-metadata">

**Author:** ![releod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9dc877/32.png) [@releod](https://www.ruby-forum.com/u/releod)\
**Post date:** [September 2, 2006, 8:47pm UTC](https://www.ruby-forum.com/t/email-validation/67363/2 "2006-09-02T20:47:00Z")

</div>

use the email\_column plugin!

It uses tmail::address for most of its lower level stuff and, as far as  
I  
can tell, this is part of the ruby action mailer.

In your model:

include EmailColumn  
email\_column :email

and it takes care of the validation

---

<div class="post-metadata">

**Author:** ![releod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9dc877/32.png) [@releod](https://www.ruby-forum.com/u/releod)\
**Post date:** [September 2, 2006, 9:27pm UTC](https://www.ruby-forum.com/t/email-validation/67363/3 "2006-09-02T21:27:03Z")

</div>

cool, works like a charm… this is the one I was using, I converted it  
from my php class a few hours ago, also works fairly well.

validates\_format\_of :email, :with =\>  
%r{^(?:[\_a-z0-9-]+)(.[\_a-z0-9-]+)_@([a-z0-9-]+)(.[a-zA-Z0-9-.]+)_(.[a-z]{2,4})$}i

I havent had any problems with it when it was PHP so im sure it will be  
perfect for most people… let me know if you know of something im  
forgetting in that regex and ill change it as needed.

---

<div class="post-metadata">

**Author:** ![releod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9dc877/32.png) [@releod](https://www.ruby-forum.com/u/releod)\
**Post date:** [September 4, 2006, 9:42am UTC](https://www.ruby-forum.com/t/email-validation/67363/4 "2006-09-04T09:42:28Z")

</div>

releod wrote:

> cool, works like a charm… this is the one I was using, I converted it  
> from my php class a few hours ago, also works fairly well.
> 
> validates\_format\_of :email, :with =\>  
> %r{^(?:[\_a-z0-9-]+)(.[\_a-z0-9-]+)_@([a-z0-9-]+)(.[a-zA-Z0-9-.]+)_(.[a-z]{2,4})$}i

It matches “anything you want\removed\_email\_address@domain.invalid”. You should use \A and \Z instead  
of ^ and $.

---

<div class="post-metadata">

**Author:** ![releod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9dc877/32.png) [@releod](https://www.ruby-forum.com/u/releod)\
**Post date:** [September 4, 2006, 2:44pm UTC](https://www.ruby-forum.com/t/email-validation/67363/5 "2006-09-04T14:44:17Z")

</div>

I agree. Check that it has an @ and at least one period somewhere  
after the @, but don’t waste any more time on it than that.  
I don’t really see why extensive email validation is necessary. The  
only reason I can think of is to stop people entering stuff which  
isn’t a real email address, but of course, that won’t work, because,  
because someone could quite easily enter removed\_email\_address@domain.invalid,  
which appears perfectly valid to any email validator regexp. So just  
leave it out… it’s pointless, and you risk excluding legitimate  
email addresses.  
-Nathan

---

<div class="post-metadata">

**Author:** ![releod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9dc877/32.png) [@releod](https://www.ruby-forum.com/u/releod)\
**Post date:** [September 4, 2006, 2:44pm UTC](https://www.ruby-forum.com/t/email-validation/67363/6 "2006-09-04T14:44:11Z")

</div>

> validates\_format\_of :email, :with =\>  
> %r{^(?:[\_a-z0-9-]+)(.[\_a-z0-9-]+)_@([a-z0-9-]+)(.[a-zA-Z0-9-.]+)_(.[a-z]{2,4})$}i

I’m pretty sure that is incorrect. Check out:  
[http://www.norid.no/domenenavnbaser/domreg.html](http://www.norid.no/domenenavnbaser/domreg.html)

Plus I believe there are military domains that are one character, like  
‘z’ for the New Zealand military?

this one actually really fries me. SO many sites don’t allow me to  
enter something like removed\_email\_address@domain.invalid

they completly crap out…sometimes it is the period before the ‘@’, or  
two periods after the ‘@’, and then the ‘.coop’ usually throws most  
people for a loop.

do people a favor and keep this validation loose unless you _really_  
need to check it. Do you care if the domain is between 2-4 chars?

Adam

---

<div class="post-metadata">

**Author:** ![releod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9dc877/32.png) [@releod](https://www.ruby-forum.com/u/releod)\
**Post date:** [September 4, 2006, 5:11pm UTC](https://www.ruby-forum.com/t/email-validation/67363/7 "2006-09-04T17:11:41Z")

</div>

On Sep 3, 2006, at 9:59 PM, Adam G. wrote:

> > validates\_format\_of :email, :with =\>  
> > %r{^(?:[\_a-z0-9-]+)(.[\_a-z0-9-]+)_@([a-z0-9-]+)(.[a-zA-Z0-9-.]  
> > +)_(.[a-z]{2,4})$}i
> 
> I’m pretty sure that is incorrect. Check out:  
> [http://www.norid.no/domenenavnbaser/domreg.html](http://www.norid.no/domenenavnbaser/domreg.html)

There is a really nice (and long) regex here that I’ve used in the past.

[http://www.regular-expressions.info/email.html](http://www.regular-expressions.info/email.html)

Robby

---

<div class="post-metadata">

**Author:** ![releod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9dc877/32.png) [@releod](https://www.ruby-forum.com/u/releod)\
**Post date:** [September 4, 2006, 11:48pm UTC](https://www.ruby-forum.com/t/email-validation/67363/8 "2006-09-04T23:48:29Z")

</div>

Hello,

Have you checked validates\_as\_email plugin:

[https://svn.greenpeace.org/repositories/rails\_plugins/validates\_as\_email/trunk/README](https://svn.greenpeace.org/repositories/rails_plugins/validates_as_email/trunk/README)

It looks quite easy to use. Personally I am having trouble installing  
it. If anybody have an idea:

ruby script/plugin install  
[https://svn.greenpeace.org/repositories/rails\_plugins/validates\_as\_email](https://svn.greenpeace.org/repositories/rails_plugins/validates_as_email)  
c:/ruby/lib/ruby/1.8/net/http.rb:588:in `connect': certificate verify failed (OpenSSL::SSL ::SSLError) from c:/ruby/lib/ruby/1.8/net/http.rb:588:in `connect’  
from c:/ruby/lib/ruby/1.8/net/http.rb:555:in `do_start' from c:/ruby/lib/ruby/1.8/net/http.rb:544:in `start’  
from c:/ruby/lib/ruby/1.8/open-uri.rb:245:in `open_http' from c:/ruby/lib/ruby/1.8/open-uri.rb:629:in `buffer\_open’  
from c:/ruby/lib/ruby/1.8/open-uri.rb:167:in `open_loop' from c:/ruby/lib/ruby/1.8/open-uri.rb:165:in `open\_loop’  
from c:/ruby/lib/ruby/1.8/open-uri.rb:135:in `open_uri' ... 11 levels... from c:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/commands/plugin.rb:871 from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in `require’  
from  
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active\_support/depende  
ncies.rb:147:in `require’  
from script/plugin:3

I have been playing around with soap4r so it could come from there.  
Did not have time to really look into it but somebody out there might  
know the answer,

Cheers,  
Carl

On 9/4/06, Robby R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> There is a really nice (and long) regex here that I’ve used in the past.
> 
> [How to Find or Validate an Email Address](http://www.regular-expressions.info/email.html)
> 
> Robby
> 
> >

–  
Carl-Gustaf H.
