views:

153

answers:

5

Using Ruby/RoR - The year is a string in the model/view. How do I validate that the user entered string is a valid gregorian calendar year?

A: 
Date.parse(string_of_date).gregorian?

Also, have a look at the documentation for the Date class.

theIV
Thanks, your suggestion doesn't work if the input is 1999 or 2005! Date.parse. is looking for YYYYMMDD, I think.
railsuser
Ah. I hadn't realized you only wanted the year (even though it says so right in the question!). My apologies.
theIV
+4  A: 

It sounds like a more direct question is: how do you validate that the user enters a string corresponding to a number between 1582 and 2500 (say). You can do this like this:

 date_string.scan(/\D/).empty? and (1582..2500).include?(date_string.to_i)

that way, you can choose what a sensible year is, too - for example, is 800 really a valid answer in your application? or 3000?

Peter
+1  A: 

Here's one way to do it:

Date.strptime(date_str, "%Y").gregorian?

Note that this will throw exceptions if the string is in an unexpected format. Another (more forgiving) option, is:

Date.new(date_str.to_i).gregorian?
Greg Campbell
+2  A: 

At the cost of a tiny bit of cleverness/regexp magic, something like the following will allow you to test not only for whether a string is numeric (the first criteria for being a valid year) but for whether it falls within a particular range of years:

def is_valid_year?(date_str, start=1900, end=2099)
  date_str.grep(/^(\d)+$/) {|date_str| (start..end).include?(date_str.to_i) }.first
end

The above function returns nil for any string with non-numeric characters, false for those which are numeric but outside the provided range, and true for valid year strings.

rcoder
+1-Wow nice work ... i think i should also look deep into Regex!
Webbisshh
A few (pedantic?) notes: You can't name a method parameter end (as that's a reserved keyword), and this method will return false for the string " 1999", so you may want to call .strip before .grep.
Greg Campbell
@Greg: good point on the parameter name -- I obviously extracted this from an IRb session, not a full source file, so names were changed in the browser window. Calling `#strip` might be a useful relaxation of the validation rule, but it would depend on the application.
rcoder