E-mail validtor?


#1

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!


#2

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 :slight_smile: It’s not probably the perfect solution, but usually it
works.

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


#3

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 :slight_smile: 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!


#4

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


#5

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’ 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


#6

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


#7

szymek wrote:

EMAIL_VALIDATION = /^\w+([.-]?\w+)@\w+([.-]?\w+)(.\w{2,3})+$/
validates_format_of :email, :with => EMAIL_VALIDATION

Pretty scary :slight_smile: 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


#8

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.


#9

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