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.
4108c0021c64d188494cae8dcfc95fd8?d=identicon&s=25 Jon Mr (jon-d)
on 2006-05-10 18: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!
F639e0cdf2fa098b535d6ec766b2aee1?d=identicon&s=25 Szymon Nowak (g0nzo)
on 2006-05-10 18: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/
4108c0021c64d188494cae8dcfc95fd8?d=identicon&s=25 Jon Mr (jon-d)
on 2006-05-10 19: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!
5f3d7eb9e9314f4eb94b4040cee4d8d2?d=identicon&s=25 Vince Puzzella (Guest)
on 2006-05-10 19:20
(Received via mailing list)
3d52a27007359841566a61e74054922b?d=identicon&s=25 Graeme Mathieson (Guest)
on 2006-05-10 19: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> 'mathie@rubaidh.com' =~ /^\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> 'mathie@woss.name' =~ /^\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" <address@example.com>' 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
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails

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

Scottish for Ruby on Rails
3d52a27007359841566a61e74054922b?d=identicon&s=25 Graeme Mathieson (Guest)
on 2006-05-10 19:23
(Received via mailing list)
On 10 May 2006, at 18:16, Vince Puzzella 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
mathie@rubaidh.com        http://www.rubaidh.com/

Scottish for Ruby on Rails
E28c35323f624b8b9ed8712e25105454?d=identicon&s=25 Ray Baxter (Guest)
on 2006-05-10 19: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:

me+1@example.com

me@example.info

me@192.168.123.100

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

--

Ray
4d8b13b54ae2fabfffd5537b04c7369c?d=identicon&s=25 Scott Fortmann-Roe (Guest)
on 2006-05-11 17: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.
91eb330fb36d1e03c856574dfb77d2bc?d=identicon&s=25 Thibaut Barrère (Guest)
on 2006-05-11 17: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.