# Validate email address

**URL:** <https://www.ruby-forum.com/t/validate-email-address/90183>\
**Category:** Rails\
**Created:** [March 26, 2007, 6:09pm UTC](https://www.ruby-forum.com/t/validate-email-address/90183 "2007-03-26T18:09:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [March 26, 2007, 6:09pm UTC](https://www.ruby-forum.com/t/validate-email-address/90183/1 "2007-03-26T18:09:22Z")

</div>

What is the best way to validate an email address?

I have been unable to fnd a validates\_email address method which seems a  
bit strange for rails

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [March 26, 2007, 6:17pm UTC](https://www.ruby-forum.com/t/validate-email-address/90183/2 "2007-03-26T18:17:05Z")

</div>

josh wrote:

> What is the best way to validate an email address?
> 
> I have been unable to fnd a validates\_email address method which seems a  
> bit strange for rails

i use this:  
validates\_format\_of :email,  
:with =\>  
/^([^@\s]+)@((?:[-a-z0-9]+.)+[a-z]{2,})$/i

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [March 26, 2007, 8:43pm UTC](https://www.ruby-forum.com/t/validate-email-address/90183/3 "2007-03-26T20:43:39Z")

</div>

david wrote:

> josh wrote:
> 
> > What is the best way to validate an email address?
> > 
> > I have been unable to fnd a validates\_email address method which seems a  
> > bit strange for rails
> 
> i use this:  
> validates\_format\_of :email,  
> :with =\>  
> /^([^@\s]+)@((?:[-a-z0-9]+.)+[a-z]{2,})$/i

thanks that seems to ahve worked fine.  
Althrough it seems a bit strange how rails does not have this in the lib

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [March 26, 2007, 8:59pm UTC](https://www.ruby-forum.com/t/validate-email-address/90183/4 "2007-03-26T20:59:25Z")

</div>

Hi –

On 3/26/07, josh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > :with =>
> > 
> > ```
> > 
> > /^([^@\s]+)@((?:[-a-z0-9]+.)+[a-z]{2,})$/i
> 
> thanks that seems to ahve worked fine.  
> Althrough it seems a bit strange how rails does not have this in the lib

If Rails were to have an email address validator, it would have to  
cover all of RFC 2822, which this regex doesn’t.

David

–  
Q. What is THE Ruby book for Rails developers?  
A. RUBY FOR RAILS by David A. Black ([http://www.manning.com/black](http://www.manning.com/black))  
(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf](http://www.rubypal.com/r4rrevs.pdf))  
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [March 26, 2007, 9:51pm UTC](https://www.ruby-forum.com/t/validate-email-address/90183/5 "2007-03-26T21:51:30Z")

</div>

On Mar 26, 1:58 pm, “David A. Black” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > > I have been unable to fnd a validates\_email address method which seems a  
> > > > If Rails were to have an email address validator, it would have to  
> > > > cover all of RFC 2822, which this regex doesn’t.

I know the book “Master Regular Expressions” by Jeffrey E.F. Friedl  
includes an example to parse an email address that is 6598 bytes long  
(see Appendix B). And I remember reading that it didn’t handle all  
possible nesting cases.

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [March 26, 2007, 10:10pm UTC](https://www.ruby-forum.com/t/validate-email-address/90183/6 "2007-03-26T22:10:34Z")

</div>

I’ve been using this plugin:

[http://agilewebdevelopment.com/plugins/validates\_email\_format\_of](http://agilewebdevelopment.com/plugins/validates_email_format_of)

Wes
