Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-08-2002, 05:25 PM   PM User | #1
ventura
New Coder

 
Join Date: Jul 2002
Location: Noblesville, IN
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
ventura is an unknown quantity at this point
Validating that start date is earlier than the end date

I have 2 fields, a start date and an end date.

how can i validate it so that the end date is later than the start date?

right now i am validating that the dates are entered in the mm/dd/yyyy format.

thanks,
__________________
Ed Ventura
Graphic/Web Designer
www.blackwatercompany.com
ventura is offline   Reply With Quote
Old 08-08-2002, 05:42 PM   PM User | #2
joh6nn
wei wu wei


 
joh6nn's Avatar
 
Join Date: Jun 2002
Location: 72° W. 48' 57" , 41° N. 32' 04"
Posts: 1,887
Thanks: 0
Thanked 1 Time in 1 Post
joh6nn is an unknown quantity at this point
can we see the code you're using right now? i know how to do it, but i'd like see what you're already doing, so that there won't be an compatability problems between what i say, and what you've got. it reduces the probability of me feeling like an idiot later. ::grin::
__________________
bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

i am a loser geek, crazy with an evil streak,
yes i do believe there is a violent thing inside of me.
joh6nn is offline   Reply With Quote
Old 08-08-2002, 05:45 PM   PM User | #3
ventura
New Coder

 
Join Date: Jul 2002
Location: Noblesville, IN
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
ventura is an unknown quantity at this point
function validateGenReport() {
var str = document.form;
var dateFormat = /\d\d\/\d\d\/\d\d\d\d/; // mm/dd/yyyy

if (str.datebox.value == '') {
alert('Please enter a start date in mm/dd/yyyy format.');
str.datebox.value = 'mm/dd/yyyy';
str.datebox.focus();
str.datebox.select();
return false;
}
else if (str.datebox.value != '') {
if (!(str.datebox.value.match(dateFormat))) {
alert('The start date:\n \n ' + str.datebox.value + ' is not in the correct format.\n \nThe date must only contain numbers and be in the following format:\n \n mm/dd/yyyy');
str.datebox.value = 'mm/dd/yyyy';
str.datebox.focus();
str.datebox.select();
return false;
}
}
if (str.datebox2.value != '') {
if (!(str.datebox2.value.match(dateFormat))) {
alert('The end date:\n \n ' + str.datebox2.value + ' is not in the correct format.\n \nThe date must only contain numbers and be in the following format:\n \n mm/dd/yyyy');
str.datebox2.value = 'mm/dd/yyyy';
str.datebox2.focus();
str.datebox2.select();
return false;
}
}
MM_showHideLayers('Layer1','','show');
//alert('Processing a report may take up to 30 seconds to complete.\n \nPlease click "OK" to start processing your report.');
}
__________________
Ed Ventura
Graphic/Web Designer
www.blackwatercompany.com
ventura is offline   Reply With Quote
Old 08-08-2002, 05:50 PM   PM User | #4
nolachrymose
Regular Coder

 
Join Date: Jun 2002
Posts: 338
Thanks: 0
Thanked 0 Times in 0 Posts
nolachrymose is an unknown quantity at this point
Code:
function dateAtts(mm,dd,yyyy) {
var d=new Date();
d.setMonth(mm+1);
d.setDate(dd);
d.setFullYear(yyyy);
return d;
}
function validDate(start,end) {
var re=/\d{2}\/\d{2}\/\d{4}/;
if(!re.test(start) || !re.test(end)) return false;

var newStart=start.split("/");
var newEnd=end.split("/");

var sDate=dateAtts(newStart[0],newStart[1],newStart[2]);
var eDate=dateAtts(newEnd[0],newEnd[1],newEnd[2]);
return (sDate.parse()<eDate.parse());
}
Hope that helps!

Happy coding!

Edit: Don't mind the line feed in the declaration of the sDate variable. For some reason it won't remove it although it shouldn't be there.

Last edited by nolachrymose; 08-08-2002 at 05:53 PM..
nolachrymose is offline   Reply With Quote
Old 08-08-2002, 06:45 PM   PM User | #5
ventura
New Coder

 
Join Date: Jul 2002
Location: Noblesville, IN
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
ventura is an unknown quantity at this point
thanks nolachrymose, but i'm not sure where i should put this code in my existing code...

thanks,
__________________
Ed Ventura
Graphic/Web Designer
www.blackwatercompany.com
ventura is offline   Reply With Quote
Old 08-09-2002, 02:58 AM   PM User | #6
RadarBob
Regular Coder

 
Join Date: Jun 2002
Location: Round Rock, Texas
Posts: 443
Thanks: 0
Thanked 0 Times in 0 Posts
RadarBob is an unknown quantity at this point
Enough of this nonsense Do y'all get paid by the line of code?

Here's all you need:
Code:
var endDate  = new Date()                      // defaults to today's date
var startDate = new Date (2002, 07, 21) // July 21, 2002

if (endDate  > startDate) {
   // the end date is later
}else{
   // the end date is at or before the start date
}
Yes! It's that simple. The miracle of object-base Javascript. The reason this simple arithmatic expression works is that dates are stored internally in milliseconds; so you can do arithmetic with dates!

Last edited by RadarBob; 08-09-2002 at 03:03 AM..
RadarBob is offline   Reply With Quote
Old 08-09-2002, 04:28 AM   PM User | #7
Catman
Regular Coder

 
Join Date: Jun 2002
Location: Ames, IA, USA
Posts: 373
Thanks: 0
Thanked 0 Times in 0 Posts
Catman is an unknown quantity at this point
My thoughts exactly, RadarBob. However, the date set in your example is actually Aug 21, 2002 rather than July 21, 2002. If ventura wants to set the date variable with values entered by users, I expect something like this might be in order:
Code:
var startDate = new Date (yyyy, mm-1, dd)
And rather than messing with that complex verification scheme and the substring extraction routine, I'd go with three text input boxes -- should simplify the code significantly.
__________________
Need more emoticons?
Visit Catman's Private Stock
Catman is offline   Reply With Quote
Old 08-09-2002, 08:53 AM   PM User | #8
mordred
Senior Coder


 
Join Date: Jun 2002
Location: frankfurt, german banana republic
Posts: 1,848
Thanks: 0
Thanked 0 Times in 0 Posts
mordred is an unknown quantity at this point
Quote:
Originally posted by RadarBob
Enough of this nonsense Do y'all get paid by the line of code?
The additional lines in nolachrymose' code are for the validation of the correct format of the data input prior to constructing the date object's, I fail to see any nonsense there.

Also, your suggestion

if (endDate > startDate)

is just some digits less than nolachrymose' example and does exactly the same:

return (sDate.parse()<eDate.parse());

Plus that you have a straight procedural code flow whereas the other example encapsulated everything in functions, which makes it better reusable.
So where's the nonsense?
mordred is offline   Reply With Quote
Old 08-09-2002, 01:17 PM   PM User | #9
RadarBob
Regular Coder

 
Join Date: Jun 2002
Location: Round Rock, Texas
Posts: 443
Thanks: 0
Thanked 0 Times in 0 Posts
RadarBob is an unknown quantity at this point
Quote:
However, the date set in your example is actually Aug 21, 2002 rather than July 21, 2002.
From The Core Javascript Guide on www.netscape.......
Quote:
With the "get" and "set" methods you can get and set seconds, minutes, hours, day of the month, day of the week, months, and years separately. There is a getDay method that returns the day of the week, but no corresponding setDay method, because the day of the week is set automatically. These methods use integers to represent these values as follows:

Seconds and minutes: 0 to 59

Hours: 0 to 23

Day: 0 (Sunday) to 6 (Saturday)

Date: 1 to 31 (day of the month)

Months: 0 (January) to 11 (December)

Year: years since 1900
RadarBob is offline   Reply With Quote
Old 08-09-2002, 01:42 PM   PM User | #10
Catman
Regular Coder

 
Join Date: Jun 2002
Location: Ames, IA, USA
Posts: 373
Thanks: 0
Thanked 0 Times in 0 Posts
Catman is an unknown quantity at this point
Yes, so 7 is August.
__________________
Need more emoticons?
Visit Catman's Private Stock
Catman is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:53 PM.


Advertisement
Log in to turn off these ads.