I''m trying to make sure that a name contains no funky characters. Is there some obvious reason why the following lines would have no effect? validates_format_of :name, :with => %r{\w*} validates_format_of :name, :with => %r{([A-Za-z])*} With these lines in my model, I can enter almost any name (with blanks, punctuation, etc.) and Rails accepts it. On the other hand, if I add validates_format_of :name, :with => %r{.*\.jpg} validates_length_of :name, :minimum => 2 then rails rejects short names or names that don''t end in .jpg. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
On Monday 25 September 2006 15:52, bburd wrote:> I''m trying to make sure that a name contains no funky characters. Is > there some obvious reason why the following lines would have no effect? > > validates_format_of :name, :with => %r{\w*} > validates_format_of :name, :with => %r{([A-Za-z])*}Because * means zero or more matches. You probably want to use + (one or more matches). -- Fraser Campbell <fraser-Txk5XLRqZ6CsTnJN9+BGXg@public.gmane.org> http://www.wehave.net/ Georgetown, Ontario, Canada Debian GNU/Linux --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Actually, I forgot the ^ and $. That''s what made this regex match almost anything. Thanks. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---