Question about validates_format_of

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.

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 C. [email protected]
http://www.wehave.net/
Georgetown, Ontario, Canada Debian
GNU/Linux

Actually, I forgot the ^ and $. That’s what made this regex match
almost anything.
Thanks.