Date validation using pattern. Why?

You need !~, not =~. Note that 0 is NOT false, it is true, so

puts "true" if 0

will print "true"

Might I suggest instead using:

    Date.parse(date)

instead? This will also make certain the dates are valid, and will accept things like 07-10-10 as well. You will need to protect that call inside of a begin ... rescue ... end block though since it will raise an exception on a format / data error.

--Michael

I'd start with: Index of Files, Classes & Methods in Ruby 3.1.2 (Ruby 3.1.2)

Scroll down in the middle column, and select "Date" to display information about that class.

--Michael

Or if he wants to use the regexp

  validates_format_of :date :with => /^[0-9]{4}[-][0-9]{2}[-][0-9]{2}$/

Michael, Thanks. Got it!

There is no info about Date.parse() in my Ruby book. Please, could you gimme URL where I can read about it.

http://www.ruby-lang.org/en/documentation/

in particular...

http://www.ruby-doc.org/core/ http://www.ruby-doc.org/stdlib/

and specifically:

http://www.ruby-doc.org/stdlib/libdoc/date/rdoc/index.html

(far right top frame, scroll to the parse method).

For myself, I keep a local copy of the above and then link directly to the method frame like this:

http://www.ruby-doc.org/core/fr_method_index.html

Then a search for 'parse' would find what I want without having all those little frames :slight_smile: