# E-mail validtor?

**URL:** <https://www.ruby-forum.com/t/e-mail-validtor/60148>\
**Category:** Rails\
**Created:** [May 10, 2006, 6:03pm UTC](https://www.ruby-forum.com/t/e-mail-validtor/60148 "2006-05-10T18:03:42Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jon-d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@jon-d](https://www.ruby-forum.com/u/jon-d)\
**Post date:** [May 10, 2006, 6:03pm UTC](https://www.ruby-forum.com/t/e-mail-validtor/60148/1 "2006-05-10T18:03:42Z")

</div>

Does anyone have a good working e-mail validator? or even some regexp to  
make the validates\_format\_of work right… that would be awesome. Also,  
maybe an online tutorial for regexp. i’ve always been afraid of it but i  
think it’s time to face the fears.

thanks!

---

<div class="post-metadata">

**Author:** ![jon-d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@jon-d](https://www.ruby-forum.com/u/jon-d)\
**Post date:** [May 10, 2006, 6:12pm UTC](https://www.ruby-forum.com/t/e-mail-validtor/60148/2 "2006-05-10T18:12:29Z")

</div>

Hi!

I found somewhere something like this (i know almost nothing about  
regexp also):

EMAIL\_VALIDATION = /^\w+([.-]?\w+)_@\w+([.-]?\w+)_(.\w{2,3})+$/  
validates\_format\_of :email, :with =\> EMAIL\_VALIDATION

Pretty scary 🙂 It’s not probably the perfect solution, but usually it  
works.

Here’s a good regexp tutorial: [http://www.regular-expressions.info/](http://www.regular-expressions.info/)

---

<div class="post-metadata">

**Author:** ![jon-d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@jon-d](https://www.ruby-forum.com/u/jon-d)\
**Post date:** [May 10, 2006, 7:12pm UTC](https://www.ruby-forum.com/t/e-mail-validtor/60148/3 "2006-05-10T19:12:43Z")

</div>

szymek wrote:

> Hi!
> 
> I found somewhere something like this (i know almost nothing about  
> regexp also):
> 
> EMAIL\_VALIDATION = /^\w+([.-]?\w+)_@\w+([.-]?\w+)_(.\w{2,3})+$/  
> validates\_format\_of :email, :with =\> EMAIL\_VALIDATION
> 
> Pretty scary 🙂 It’s not probably the perfect solution, but usually it  
> works.
> 
> Here’s a good regexp tutorial: [http://www.regular-expressions.info/](http://www.regular-expressions.info/)

hey thanks! worked like a charm!

---

<div class="post-metadata">

**Author:** ![jon-d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@jon-d](https://www.ruby-forum.com/u/jon-d)\
**Post date:** [May 10, 2006, 7:20pm UTC](https://www.ruby-forum.com/t/e-mail-validtor/60148/4 "2006-05-10T19:20:21Z")

</div>

[http://www.agilewebdevelopment.com/plugins/show/20](http://www.agilewebdevelopment.com/plugins/show/20)

---

<div class="post-metadata">

**Author:** ![jon-d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@jon-d](https://www.ruby-forum.com/u/jon-d)\
**Post date:** [May 10, 2006, 7:23pm UTC](https://www.ruby-forum.com/t/e-mail-validtor/60148/5 "2006-05-10T19:23:28Z")

</div>

Hi,

OK, that particular regexp assumes you have a 2 or 3 character TLD on  
the address:

irb(main):004:0\> ‘removed\_email\_address@domain.invalid’ =~ /^\w+([.-]?\w+)_@\w+  
([.-]?\w+)_(.\w{2,3})+$/  
=\> 0

Unfortunately, that doesn’t work for \*.info and \*.name email address,  
like my own personal email address for example:

```
 irb(main):003:0> 'removed_email_address@domain.invalid' =~ /^\w+([\.-]?\w+)*@\w+

```

([.-]?\w+)\*(.\w{2,3})+$/  
=\> nil

So, how would we construct an accurate, definitive check? Looking at  
the EBNF in the relevant RFCs (2821 and 2822), I previously came up  
with the following (it was written in Python; I’m converting to ruby  
on the fly so it needs checking):

def validate\_address(addr)  
atext = ‘0-9a-zA-Z!#$%&'\*+\_/=?^-`{|}~’  
subdomain = '[0-9a-zA-Z][0-9a-zA-Z-]_[0-9a-zA-Z]’  
localpart, domain = addr.split ‘@’, 2  
return false if domain.empty?  
return false unless localpart =~ /^([#{atext}][.#{atext}]_|“[^\”]  
\*")$/  
return false unless domain =~ /^#{subdomain}(.#{subdomain})+/  
return true  
end

You could chuck that into the completely unreadable single regexp:

EMAIL\_VALIDATION = /^([0-9a-zA-Z!#$%&'\*+\_/=?^-`\{|\}~][.0-9a- zA-Z!#\$%\&\'\*\+_\/=\?^\-`{|}~]_|“[^\”]_")@[0-9a-zA-Z][0-9a-zA-Z-]  
\*0-9a-zA-Z+$/

Note that this is only checking the email address itself. Bear in  
mind that if you’re dealing with email, rather than just getting an  
address from a form, you have to deal with parsing out the address  
from the ‘“Name” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)’ string.

Cheers,

Graeme

On 10 May 2006, at 17:12, szymek wrote:

> Here’s a good regexp tutorial: [http://www.regular-expressions.info/](http://www.regular-expressions.info/)
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Graeme Mathieson, Technical Director, Rubaidh Ltd  
removed\_email\_address@domain.invalid [http://www.rubaidh.com/](http://www.rubaidh.com/)

Scottish for Ruby on Rails

---

<div class="post-metadata">

**Author:** ![jon-d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@jon-d](https://www.ruby-forum.com/u/jon-d)\
**Post date:** [May 10, 2006, 7:23pm UTC](https://www.ruby-forum.com/t/e-mail-validtor/60148/6 "2006-05-10T19:23:42Z")

</div>

On 10 May 2006, at 18:16, Vince P. wrote:

> [http://www.agilewebdevelopment.com/plugins/show/20](http://www.agilewebdevelopment.com/plugins/show/20)

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

## Ooh, that’s a much more readable way of showing what I was trying to say. Thanks!

Graeme Mathieson, Technical Director, Rubaidh Ltd  
removed\_email\_address@domain.invalid [http://www.rubaidh.com/](http://www.rubaidh.com/)

Scottish for Ruby on Rails

---

<div class="post-metadata">

**Author:** ![jon-d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@jon-d](https://www.ruby-forum.com/u/jon-d)\
**Post date:** [May 10, 2006, 7:33pm UTC](https://www.ruby-forum.com/t/e-mail-validtor/60148/7 "2006-05-10T19:33:18Z")

</div>

szymek wrote:

> EMAIL\_VALIDATION = /^\w+([.-]?\w+)_@\w+([.-]?\w+)_(.\w{2,3})+$/  
> validates\_format\_of :email, :with =\> EMAIL\_VALIDATION
> 
> Pretty scary 🙂 It’s not probably the perfect solution, but usually it  
> works.

That pattern doesn’t match:

removed\_email\_address@domain.invalid

removed\_email\_address@domain.invalid

[me@192.168.123.100](mailto:me@192.168.123.100)

Here is a page devoted to regexps and e-mail addresses:

> **[Regular Expression Library](https://regexlib.com/DisplayPatterns.aspx?AspxAutoDetectCookieSupport=1)**
>
> Regular Expression Library provides a searchable database of regular expressions. Users can add, edit, rate, and test regular expressions.

–

Ray

---

<div class="post-metadata">

**Author:** ![jon-d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@jon-d](https://www.ruby-forum.com/u/jon-d)\
**Post date:** [May 11, 2006, 5:16pm UTC](https://www.ruby-forum.com/t/e-mail-validtor/60148/8 "2006-05-11T17:16:58Z")

</div>

I definitely feel that this is a common enough issue, and an ideal  
solution is complex enough that a validates\_emailness\_of method should  
be built into rails.

---

<div class="post-metadata">

**Author:** ![jon-d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@jon-d](https://www.ruby-forum.com/u/jon-d)\
**Post date:** [May 11, 2006, 5:20pm UTC](https://www.ruby-forum.com/t/e-mail-validtor/60148/9 "2006-05-11T17:20:34Z")

</div>

although not built-in into Rails, the plugin allows to write:

class MyClass \< ActiveRecord::Base  
validates\_presence\_of :email  
validates\_as\_email :email  
end

nice!

cheers

Thibaut
