input day in the past

A

avcc

Hello,

If have a HTML form where a user can type a date, this date must be today,
or in the future. To test it i made this javascript

function validate()
{
var cdate_h = form.elements["cdate"].value;


if (cdate_h.length == 0)
{
alert("completion date");
return;
}

calDate = new Date();

var day = calDate.getDate();
var month = calDate.getMonth() + 1;
var year = calDate.getFullYear();

var today = year * 10000 + month * 100 + day;

var pos1=cdate_h.indexOf("-"); // date
format YYY-MM-DD
var pos2=cdate_h.indexOf("-",pos1+1);
var strYear=cdate_h.substring(0,pos1);
var strMonth=cdate_h.substring(pos1+1,pos2);
var strDay=cdate_h.substring(pos2+1);

var userdate = strYear * 10000 + strMonth * 100 + strDay;

if (parseFloat(userdate) < parseFloat(today))
{
alert ("Completion date can not be in the past");
return;
}
}

When I debug the variables userdate and today it looks fine, but the if
statement does not work WHY ??????

Can someone help me ? Many thanks in advance.

Richard
 
L

Lasse Reichstein Nielsen

avcc said:
var userdate = strYear * 10000 + strMonth * 100 + strDay;

In this, strDay is a string, so it as appended to userdate to form a
new string.
If strYear is "2003", strMonth is "10" and strDay is "18", then the result
is
(20030000 + 1000 + "18") = "2003100018"
It is not the expected 20031018.

If you change the above line to
var userdate = strYear * 10000 + strMonth * 100 + (+strDay);
of
var userdate = strYear * 10000 + strMonth * 100 + strDay * 1;
then userdate becomes a number, and the one you expect.
if (parseFloat(userdate) < parseFloat(today))

You can drop parseFloat on today, it is already a number. If you make
the fix above, so is userdate.

/L
 
R

Richard Cornford

avcc said:
If have a HTML form where a user can type a date, this date
must be today, or in the future. To test it i made this javascript

Generally:-
function validate()
{
var cdate_h = form.elements["cdate"].value;
if (cdate_h.length == 0)
{
alert("completion date");
return;
}
calDate = new Date();

var day = calDate.getDate();
var month = calDate.getMonth() + 1;
var year = calDate.getFullYear();
var today = year * 10000 + month * 100 + day;

var pos1=cdate_h.indexOf("-"); // date format YYY-MM-DD
var pos2=cdate_h.indexOf("-",pos1+1);
var strYear=cdate_h.substring(0,pos1);
var strMonth=cdate_h.substring(pos1+1,pos2);
var strDay=cdate_h.substring(pos2+1);

Specifically, the values of strMonth, strYear and strDay are strings. In
the next line:-
var userdate = strYear * 10000 + strMonth * 100 + strDay;

- the use of the multiplication operator will automatically type-convert
strYear and strMonth to numbers but the + operator is a duel purpose
addition and string concatenation operator and if either of its operands
is a string it does concatenation. strDay is a string so even if
((strYear*10000)+(strMonth*100)) produced a numeric value the final +
operator will have that value converted into a string and concatenate
strDay to it. Later using parsFloat (on an integer?) will have that
string converted back to a number for the comparison but that string is
one to two characters longer than you are expecting thus the number is
at least 10 to 100 times larger.
if (parseFloat(userdate) < parseFloat(today))
{
alert ("Completion date can not be in the past");
return;
}
}

Richard.
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen
If have a HTML form where a user can type a date, this date must be today,
or in the future. To test it i made this javascript

function validate()
{
var cdate_h = form.elements["cdate"].value;


if (cdate_h.length == 0)
{
alert("completion date");
return;
}

calDate = new Date();

var day = calDate.getDate();
var month = calDate.getMonth() + 1;
var year = calDate.getFullYear();

var today = year * 10000 + month * 100 + day;

var pos1=cdate_h.indexOf("-"); // date
format YYY-MM-DD
var pos2=cdate_h.indexOf("-",pos1+1);
var strYear=cdate_h.substring(0,pos1);
var strMonth=cdate_h.substring(pos1+1,pos2);
var strDay=cdate_h.substring(pos2+1);

var userdate = strYear * 10000 + strMonth * 100 + strDay;

if (parseFloat(userdate) < parseFloat(today))
{
alert ("Completion date can not be in the past");
return;
}
}

When I debug the variables userdate and today it looks fine, but the if
statement does not work WHY ??????

Can someone help me ? Many thanks in advance.


The last thing that is wrong is that you did not tell us explicitly how
it did not work. Perhaps it works for some combinations of dates.

A further error is that, in English, you should be writing "cannot"
instead of "can not". The words "can not" really mean "possibly not",
whereas "cannot" means "certainly not". This is a European secret which
the Americans have not yet discovered.

The previous thing is that, after finding (I suppose) that the alert did
or did not occur, you should immediately have preceded it with, for
example, alert(userdate+'\n'+today) , in order to see what had
happened.

The explanation of what happens has already been given.

The code is too long. AIUI, cdate_h is a string in YMD format.
Assuming it has a 4-digit year, consider

S = "2003-01-05"
if ( new Date() - new Date(S.replace(/-/g, '/')) > 0 )
alert("Completion date cannot be in the past")

If it may be a 2-digit year,
S = S.replace(/(\d+)\D+(\d+)\D+(\d+)/, '$2/$3/$1')
will generate a DDF M/D/Y which I believe all Javascript will
understand.

But if it will be a 2-digit year, then, for a while, use
S = '20'+S.replace(/-/g, '/')
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top