Wednesday, October 14, 2009

Javascript Date Validation using toString method

If you want to test date objects that are initialized by input from forms (particularly text boxes), the toString method comes handy. But beware, Javascript's Date.toString() function returns different values in IE and Firefox if the object is initiated with invalid date.


For example:
var d = new Date("garbage");
alert(d.toString());


Returns
1. "NaN" (stands for Not a Number) in IE and
2. "Invalid Date" in Firefox


Here's a list of all methods for Date objects: http://www.javascriptkit.com/jsref/date.shtml
Also, if you're looking for a jquery plugin for a popup calendar, I'd recommend using Kevin Luck's date-picker (http://www.kelvinluck.com/assets/jquery/datePicker/v2/demo/). We used this at a client and it worked consistently across IE, FireFox and Chrome.

No comments:

Post a Comment