[thelist] validating date strings with javascript
Hershel Robinson
hershelr at netvision.net.il
Thu Feb 20 13:21:50 CST 2003
> So I have the typical date field in a form. It should be entered in
> the mm/dd/yyyy format.
>
> I have a function that works - except for one thing. If you enter:
>
> 1/12/2003 or 1/1/2003
>
> my function will throw an error because the leading '0' or century
> has not been put in (Right now I'm merely stripping extra chars and
> counting the numbers, less than 8 - kaput).
>
> I'd like to automatically add in the zeros if they only input one
> digit for day or month (or both). The problem is this date only gets
> entered in ONE field (not 2 or three) and I cannot change that - so
> you get one value just like I typed above (sans slashes if you want
> it).
Why not create a Date object and set it to the inputted date; then you can
have JS test for you if it's a valid date as well. If it is valid, then you
can extract the month, day and year and append them together in any format
you like.
Just an idea. :)
Hershel
More information about the thelist
mailing list