Jump to content


Photo

any idea why this validation is failing?


  • Please log in to reply
3 replies to this topic

#1 micah1701

micah1701
  • Members
  • PipPipPip
  • Advanced Member
  • 613 posts
  • LocationEllington, CT USA

Posted 13 July 2006 - 03:35 PM

I have a calendar application that allows for repeating events.
the user enters a begin date AND an end date.
as a client side check to make sure the user doesn't enter an end date that occurs before the start date (or the same day as the start date) I run the following function onSubmit.

if(document.getElementById('noRepeat').checked == false) //if event repeats
 {
 if(document.getElementById('endYear').value <= document.getElementById('year').value
  &&
    document.getElementById('endMonth').value <= document.getElementById('month').value
  &&
    document.getElementById('endDay').value <= document.getElementById('day').value)
	{
	alert("Please select a future End Date for this repeating event");
	return false;
	}
 }

however, even when I enter an endDate in the future, it triggers my alert().
any thoughts?


"Confidence in the face of risk."

#2 janroald

janroald
  • Members
  • PipPipPip
  • Advanced Member
  • 78 posts
  • LocationTrondheim, Norway

Posted 13 July 2006 - 03:46 PM

Your date comparison is all wrong. Say enddate is 2007/06/07 and startdate is 2006/05/06, then your comparison would be

if(2006 <= 2007 && 06 <= 05 && 07 <= 06) {
                                ^                ^
Both month and day will return a false even though your date is in the future.
I dont have time to show you how to do this right now, but googling some "javascript date comparison" or similar should get you started.

#3 micah1701

micah1701
  • Members
  • PipPipPip
  • Advanced Member
  • 613 posts
  • LocationEllington, CT USA

Posted 13 July 2006 - 03:57 PM

Your date comparison is all wrong. Say enddate is 2007/06/07 and startdate is 2006/05/06, then your comparison would be

if(2006 <= 2007 && 06 <= 05 && 07 <= 06) {
                                ^                ^
Both month and day will return a false even though your date is in the future.


I see your point that the month and day will return false, but that is ok.
my logic is that the if statement should only return true if all 3 of the criteria are met. the month and day may fail, but the year doesnt' - so therefore, the whole if statement should return false and not prompt my error.

so if my enddate is 2007/July/05 and my startdate is 2006/June/07, then my comparison would be

if(2007 <= 2006 && June <= July && 5 <= 7) {
        pass                fail                fail

I'm still don't understand where my logic is flawed.

EDIT: I did, however work around this whole issue by just using the date object
if(document.getElementById('noRepeat').checked == false) //if event repeats
 {
var startDate = new Date;
startDate.setDate(document.getElementById('day').value);
startDate.setMonth(document.getElementById('month').value);
startDate.setYear(document.getElementById('year').value);
var endDate = new Date;
endDate.setDate(document.getElementById('endDay').value);
endDate.setMonth(document.getElementById('endMonth').value);
endDate.setYear(document.getElementById('endYear').value);
 if(endDate <= startDate){
	alert("Please select a future End Date for this repeating event \n " + x.endYear.value + " " + x.endMonth.value + " " + x.endDay.value + " " + x.year.value + " " + x.month.value + "  " + x.day.value );
	return false;
	}
 }

"Confidence in the face of risk."

#4 janroald

janroald
  • Members
  • PipPipPip
  • Advanced Member
  • 78 posts
  • LocationTrondheim, Norway

Posted 14 July 2006 - 08:43 AM

I see you have found the right way to do it. Great work.

I see there was a flaw in my logic also, but since you have the solution I wont bother with the details as of why your logic failed. It was probably a string/integer comparison problem or something other absurd js-typical hassle. The way you do it now is the correct way.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users