rubyonrailsin

A Ruby and Rails talk

Wednesday, March 10, 2010


[Rails] Re: Is there a rails way to validate format of an email addr

by rubyonrailsin 0 comments



Share this post:
Design Float
StumbleUpon
Reddit

Robert Walker wrote:
> Rajinder Yadav wrote:
>> How can I validate and email address without using complex reg-ex is
>> there a simple rails way to do this?
>
> Are you nuts? That's just the sort of thing Regexp was designed to do.
> And, it's not really all that complex.

Yes it is. The *only* correct regexps I am aware of for e-mail
addresses are on the order of a page in length. All shorter regexps
reject some valid e-mail addresses.

Just check for the . and @, and trust the user for the rest.

> The Rails docs even use email
> validation as the example for validates_format_of so it's not like you
> have to write the "complex" regex yourself. Just copy-and-paste:
>
> class Person < ActiveRecord::Base
> validates_format_of :email, :with =>
> /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create
> end

That regexp is just a simple example. It isn't even close to covering
all valid e-mail addresses. Don't use it.

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment

Subscribe feeds via e-mail

Blog Archive