473,378 Members | 1,407 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

input day in the past

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

Jul 20 '05 #1
3 2567
"avcc" <av**@freeler.nl> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
"avcc" <av**@freeler.nl> wrote in message
news:Nn**********************@amsnews03.chello.com ...
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:-
<URL: http://www.merlyn.demon.co.uk/js-dates.htm >
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.
Jul 20 '05 #3
JRS: In article <Nn**********************@amsnews03.chello.com>, seen
in news:comp.lang.javascript, avcc <av**@freeler.nl> posted at Sat, 18
Oct 2003 10:14:05 :-
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, '/')

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: zhi | last post by:
Really confused, when I use keyword style argument as following: >>> input(prompt="hello") Traceback (most recent call last): File "<pyshell#52>", line 1, in -toplevel- input(prompt="hello")...
6
by: Dave Reid | last post by:
Hi everyone... I'm pretty much a newbie C++ user, and I've run into a problem. I'm trying to read in a large text file, and then do manipulations on it. I can read it into a large 2-dimensional...
3
by: Ben | last post by:
Here's my form: <form name="aForm" method='post'> <input type=file name=file1 onkeypress='KeyPress()'><br> <a id='attachMoreLink' href='javascript:AddFileInput()">Attach More Files </a> <input...
30
by: Richard | last post by:
Level: Java newbie, C experienced Platform: Linux and Win32, Intel Another programmer and I are working on a small project together. He's writing a server process in Java that accepts input...
1
by: alw0015 | last post by:
I'm working on a piece of code that reads in a time from the user. This time value consists of 3 separate inputs: 1 integer representing hours 1 char representing the ":" 1 integer...
3
by: cbradio | last post by:
Hi, I am having trouble developing a form in a restricted environment. My sample code is found below my message (sorry I don't have a URL). Basically, without a doctype, the form displays properly...
18
by: Diogenes | last post by:
Hi All; I, like others, have been frustrated with designing forms that look and flow the same in both IE and Firefox. They simply did not scale the same. I have discovered, to my chagrin,...
59
by: David Mathog | last post by:
Apologies if this is in the FAQ. I looked, but didn't find it. In a particular program the input read from a file is supposed to be: + 100 200 name1 - 101 201 name2 It is parsed by reading...
209
by: arnuld | last post by:
I searched the c.l.c archives provided by Google as Google Groups with "word input" as the key words and did not come up with anything good. C++ has std::string for taking a word as input from...
27
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
I have a fully-portable C program (or at least I think I do). It works fine on Windows, but malfunctions on Linux. I suspect that there's something I don't know about the standard input stream...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.