ventura 08-08-2002, 05:25 PM 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,
joh6nn 08-08-2002, 05:42 PM 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::
ventura 08-08-2002, 05:45 PM 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.');
}
nolachrymose 08-08-2002, 05:50 PM 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! :)
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.
ventura 08-08-2002, 06:45 PM thanks nolachrymose, but i'm not sure where i should put this code in my existing code...
thanks,
RadarBob 08-09-2002, 02:58 AM Enough of this nonsense:mad: Do y'all get paid by the line of code?
Here's all you need:
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!
Catman 08-09-2002, 04:28 AM 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:
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.
mordred 08-09-2002, 08:53 AM Originally posted by RadarBob
Enough of this nonsense:mad: 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?
RadarBob 08-09-2002, 01:17 PM 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.......
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
Catman 08-09-2002, 01:42 PM Yes, so 7 is August.
|
|