Forum: Ruby on Rails E-mail validtor??

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jon M. (Guest)
on 2006-05-10 20:03
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!
Szymon N. (Guest)
on 2006-05-10 20:12
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/
Jon M. (Guest)
on 2006-05-10 21:12
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/

hey thanks! worked like a charm!
Vince P. (Guest)
on 2006-05-10 21:20
(Received via mailing list)
Graeme Mathieson (Guest)
on 2006-05-10 21:23
(Received via mailing list)
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](\.[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>' string.

Cheers,

Graeme

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

>
> Here's a good regexp tutorial: http://www.regular-expressions.info/
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails

--
Graeme Mathieson, Technical Director, Rubaidh Ltd
removed_email_address@domain.invalid        http://www.rubaidh.com/

Scottish for Ruby on Rails
Graeme Mathieson (Guest)
on 2006-05-10 21:23
(Received via mailing list)
On 10 May 2006, at 18:16, Vince P. wrote:

> http://www.agilewebdevelopment.com/plugins/show/20

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/

Scottish for Ruby on Rails
Ray B. (Guest)
on 2006-05-10 21:33
(Received via mailing list)
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

Here is a page devoted to regexps and e-mail addresses:
http://regexlib.com/DisplayPatterns.aspx

--

Ray
Scott Fortmann-Roe (Guest)
on 2006-05-11 19:16
(Received via mailing list)
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.
Thibaut Barrère (Guest)
on 2006-05-11 19:20
(Received via mailing list)
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
This topic is locked and can not be replied to.