You are viewing this page out of context. To see it in the context it is intended please click here.
About This Page
Reply posted by Melissa Snell on Tue 17 Oct 2006 in response to Form Validator R3.0
Found a bug in both this version and v2.0
This is one of those bugs that only a user could find! It only occurs in Augustand September!!
In your v2.0 template (in the validDate function) and perhaps also in this
version, you use this kind of thing to create date objects to see if the date
entered is within the expected bounds:
var theDate = new Date(dateBits[2], parseInt(dateBits[1]) - 1, dateBits[0]);
dateBits contains the day, month and years respectively.
So when the user is working on a form and today's date is in September (lets
say, the 24th) dateBits would contain:
dateBits[0]="2006"
dateBits[1]="09"
dateBits[2]="24"
However, the date object ends up being 24th December 2005!!!!
Why does this happen? Well there is a 'bug' in parseInt that means that if you
use it to convert a string with a leading zero, it assumes it is dealing with
OCTAL (base 8) numbers : 08 and 09 are therefore not valid numbers, so parseInt
returns zero. Zero is January, so when your code then subtracts -1 it becomes
December of the previous year!! I won't go into great detail with regard to the
swearing involved in tracking this one down....
The fix for this is very simple: you simply tell parseInt that you are working
in base 10 like this:
parseInt(dateBits[1], 10)
Hey presto! parseInt says "oh - you mean NINE!"