Forum Moderators: open

Message Too Old, No Replies

validating date

         

crowndlager

5:51 am on Sep 26, 2004 (gmt 0)

10+ Year Member



ive got this function here

function todaytxt() {
var Today=new Date();
return Today.getMonth()+1+"/"+Today.getDate()+"/"+Today.getFullYear();

and within the <body> i have
Date: <input class="numbers" name="formdate" size="10" disabled>

now my problem is, i have a form where i want people to put a valid date in, which isnt prior to current date and no longer than 30days after current date, but i have no idea how to go about this, any help would be appreciated.

Birdman

11:56 am on Sep 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I found out the hard way that you shouldn't use JavaScript to validate dates entered by the user. Why you ask? Because WAY too many of them do not have their clock(including date) set properly.

Server-side scripting is the best way to do it.

Sorry I didn't really answer your question.

Birdman

PS: Welcome to Webmaster World!

BjarneDM

9:58 am on Sep 27, 2004 (gmt 0)

10+ Year Member



Whenever I get this problem of finding out whether a given date/time is between an upper and a lower bound I always convert the three times in question to either the number of days or the number of milliseconds according to which functions are available in the programming language in question.

In your question, I'ld do the following:


var lowDate = new Date();
lowDate.setHours(0); lowDate.setMinutes(0); lowDate.setSeconds(0);
var highDate = new Date(lowDate.getTime()+31*834000);
currentDate = new Date(formYear,formMonth,formDay,12,0,0)
var validDate =
(lowDate.getTime()<currentDate.getTime() &&
currentDate.getTime()<highDate.getTime())
? true : false ;

leaving out testing whether the user input is a legitimate date.

A reference to the various constructors, properties and methods of the Date object in java/ecma-script can be found here: [devguru.com...]

crowndlager

7:58 am on Sep 28, 2004 (gmt 0)

10+ Year Member



thanks for input BjarneDM

cheers