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! -- Posted via http://www.ruby-forum.com/.
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/
--
Posted via http://www.ruby-forum.com/.
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! -- Posted via http://www.ruby-forum.com/.
http://www.agilewebdevelopment.com/plugins/show/20 On 5/10/06 1:12 PM, "Jon Mr" <jon.druse@gmail.com> wrote:> 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!
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:
> 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/
>
> --
> 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
On 10 May 2006, at 18:16, Vince Puzzella wrote:> http://www.agilewebdevelopment.com/plugins/show/20https://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
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
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. On 5/10/06, Ray Baxter <ray@warmroom.com> wrote:> 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 > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Easily help charity when you shop: www.GiveTeam.org I''m a member of the Give Team, are you?
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 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060511/50fd1059/attachment-0001.html