[thelist] validating date strings with javascript

Hershel Robinson hershelr at netvision.net.il
Thu Feb 20 13:53:21 CST 2003


> Hm, well the problem is if they don't input it correctly - say they
> input 1/1/2000 - I still haven't taken care of the 'leading zero'
> problem, so I still don't know what the date is. For all I know its
> 1/12/000 and they forgot the 2 - especially if they didn't enter any
> delimeters ala 112000...

Someone who enters 1/1/2000 and meant 1/12/2000 has made a typographical
error and no script in the world can correct that.

The question of 112000 is precisely why I suggested using a Date object.  I
believe you can set the Date object to that string and then test to see if
it is a valid date:

   var theDate = new Date(dateText)
   if (isNaN(theDate)) {
      alert("You have entered an invalid date.  Please use the format
MM/DD/YYYY.")
      return false
   }

Or something like that. :)

Hershel




More information about the thelist mailing list