Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Start/End Date validation 1

Status
Not open for further replies.

JohnShell

Technical User
Aug 29, 2003
35
0
0
US
Hi,

Want to ensure end date(s) do not precede start date(s). There are 5 text inputs for begin and end dates. They are:
startdate1; startdate2; startdate3; startdate4; RCbeginDate; stopdate1; stopdate2; stopdate3; stopdate4; and, RCendDate.

Of course, what I have below does not work. Any suggestions.
Call the function with this:

<cfform name="RAWH"
action="actionPage.cfm"
onsubmit="return Validation(); return Clear(); return ValiDate();"
method="post">

function ValiDate()
{
var SDate1 = document.getElementById(startdate1).value;
var SDate2 = document.getElementById(startdate2).value;
var SDate3 = document.getElementById(startdate3).value;
var SDate4 = document.getElementById(startdate4).value;
var SDate5 = document.getElementById(RCbeginDate).value;
var EDate1 = document.getElementById(stopdate1).value;
var EDate2 = document.getElementById(stopdate2).value;
var EDate3 = document.getElementById(stopdate3).value;
var EDate4 = document.getElementById(stopdate4).value;
var EDate5 = document.getElementById(RCendDate).value;
var alertReason = 'End Date must be equal to or greater
than Begin Date.';
var endDate1 = new Date(EDate1);
var startDate1 = new Date(SDate1);
var endDate2 = new Date(EDate2);
var startDate2 = new Date(SDate2);
var endDate3 = new Date(EDate3);
var startDate3 = new Date(SDate3);
var endDate4 = new Date(EDate4);
var startDate4 = new Date(SDate4);
var endDate5 = new Date(EDate5);
var startDate5 = new Date(SDate5);

if(SDate1 != '' && EDate1 != '' && startDate1 > endDate1){alert(alertReason);
if(SDate2 != '' && EDate2 != '' && startDate2 > endDate2){alert(alertReason);
if(SDate3 != '' && EDate3 != '' && startDate3 > endDate3){alert(alertReason);
if(SDate4 != '' && EDate4 != '' && startDate4 > endDate4){alert(alertReason);
if(SDate5 != '' && EDate5 != '' && startDate5 > endDate5){alert(alertReason);
return false;}
}

Thank you - JS
 
Hi,

Got some help locally and significantly revamped the above code:

//validate entered date values - end date cannot preceed begin date
function compareDate(date1,date2)
{
var endDate = new Date(date2);
var startDate = new Date(date1);

if(startDate > endDate){return true;}
else
{
return false;
}
}

Have another function calling the above and using this:

if ((SD1.value.length > 0) && (ED1.value.length > 0))
{
if (compareDate(SD1.value,ED1.value))
{
alert('End Date must be equal to or greater than Begin Date.');
ED1.focus();
return false;
}
}

Thanks anyway.
 
Well it seems that you've found the solution by yourself but let me suggest you a very good library for handling advanced date validations/calulations etc...

date.js

just google it and you'll get the idea!

Brgds,

g.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top