Your statement of, "I wanted to validate text inputed by a user as they were typing to tell them if it was a valid date."
seems to be a bit misleading. I was under the impression that your code would start off returning
until I entered a non-number character, invalid separator, or invalid range for the month/day/year position I was in. However, your method doesn't appear to do this. I've written a test program and wired up a textbox to the
event to call your method and it only returns
when a complete and accurate date has been entered.
I think there are many ways to do this so I'm surprised you didn't find an existing example that met your needs. Personally, I use a regular expression. The below regular expression even handles leap years.
var dateRegEx = new RegExp(/^(?:(?:(?:0?|1)(\/|-)31)|(?:(?:0?[1,3-9]|1[0-2])(\/|-)(?:29|30)))(\/|-)(?:[1-9]\d\d\d|\d[1-9]\d\d|\d\d[1-9]\d|\d\d\d[1-9])$|^(?:(?:0?[1-9]|1[0-2])(\/|-)(?:0?[1-9]|1\d|2[0-8]))(\/|-)(?:[1-9]\d\d\d|\d[1-9]\d\d|\d\d[1-9]\d|\d\d\d[1-9])$|^(0?2(\/|-)29)(\/|-)(?:(?:000|00|00)|(?:\d\d)?(?:0||))$/);
I did not write this regular expression myself so I do not take credit for it. I don't recall where I found it, but it was years ago and I haven't had any problems with it in any of my applications.
I've also used Prerak Patel alternate method when I have separate form fields for each date part.