IntelliJ Java IDE
The moose likes HTML, CSS and JavaScript and the fly likes validating  Dates Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Practical Unit Testing with TestNG and Mockito this week in the Testing forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Reply Bookmark "validating  Dates" Watch "validating  Dates" New topic
Author

validating Dates

Ross Varlese
Greenhorn

Joined: Jan 27, 2003
Posts: 6
Hi
I need to validate two date inputs in the DD/MM/YYYY format: start_date and end_date. Obviously the start_date cannot after the end_date (or end_date before the start_date).
I am using the following piece of code:
function checkMForm ( form ) {
StartDate = new Date(form.start_date.value);
EndDate = new Date(form.end_date.value);

ddFrom=StartDate.getDate();
ddTo=EndDate.getDate();


mmFrom=StartDate.getMonth();
mmTo=EndDate.getMonth();
mmFrom+=1;
mmTo+=1;

yyyyFrom = StartDate.getFullYear();
yyyyTo=EndDate.getFullYear();

if (ddFrom>ddTo && yyyyTo==yyyyFrom && mmTo==mmFrom)
{
alert("Invalid Date: Day");
return(false);
}
else if(mmFrom>mmTo && yyyyTo==yyyyFrom)
{
alert("Invalid Date: Month");
return(false);
}

else if (yyyyFrom>yyyyTo)
{
alert("Invalid Date: Year");
return(false);
}

return (true) ;
}
At first glimpse this appeared to work until the start_date was at the end of one month and the end_date at the being on the next. Then it displayed the error message �invalid Date: year� even though it was a perfectly valid entry.
Can anyone help with this code or perhaps suggest some other way of checking dates?
Cheers
Ross
sunitha reghu
Ranch Hand

Joined: Dec 12, 2002
Posts: 937
Ross,
U need lot of other validations also like if suppose a date like this 02/30/20003 which is not correct. I have written a js code to validate date in the format mm/dd/yyyy which also checks start date can not be after end date. if u need i can mail it u
Ross Varlese
Greenhorn

Joined: Jan 27, 2003
Posts: 6
Yes please, would be a great help.
cheers.
r.varlese@derby.ac.uk
sunitha reghu
Ranch Hand

Joined: Dec 12, 2002
Posts: 937
mailed...check mailbox
Bear Bibeault
Author and opinionated walrus
Marshal

Joined: Jan 10, 2002
Posts: 50673

mailed...check mailbox

Well I'm sure that's very helpful to others that will have the same question.
bear


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Yuriy Fuksenko
Ranch Hand

Joined: Feb 02, 2001
Posts: 411
Since product, that I am working on supports all kind of different locales and date formats,
I prefer to validate Date on a server side.
sunitha reghu
Ranch Hand

Joined: Dec 12, 2002
Posts: 937
Originally posted by Bear Bibeault:

Well I'm sure that's very helpful to others that will have the same question.
bear

Bear ..the reason why i didnt put the code that code is according to naming conventions i used in the form even the alert messages.
i can post the code no problem..im too lazy 2day to change the alert messages.....Then another thing wat i ahd done i took the system date upfront coz there is a probality that users can change the date in their pc...
one function in the code checks the birth date is less than start date whcih is not required for anyone so u can take that off.
sunitha reghu
Ranch Hand

Joined: Dec 12, 2002
Posts: 937
not allwoing me to post the code
coz of html tag
i can mail u the code and u can post the code
Bear Bibeault
Author and opinionated walrus
Marshal

Joined: Jan 10, 2002
Posts: 50673

Yeah, that is a pain sometimnes, isn't it?
Perhaps you could summarize what fixed the problem for you for the benefit of future searchers of this topic?
bear
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15003
Okay, This is it, I will make a utility tat will escape the code for this website so it will be easier for people to post. I should have it up and running fully by tom afternoon. (EST)
I will see if I can get it linked on top page of this forum.
Eric
Bear Bibeault
Author and opinionated walrus
Marshal

Joined: Jan 10, 2002
Posts: 50673

I will make a utility tat will escape the code for this website

you == forum hero

bear
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15003
I got permission to do it, I will see if I get it done tonight, and have it up in a couple of days.
All it is is some regular exp. with replace.
What Fun!
Eric
sunitha reghu
Ranch Hand

Joined: Dec 12, 2002
Posts: 937
Thanks Eric.....
 
IntelliJ Java IDE
 
subject: validating Dates
 
Threads others viewed
is that any utility class that help compare date within start and end date ?
Subtract 2 Dates
how to solve this insert record problem ?
New to Java - i need help please!
Date Object i.e vs firefox
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture