micah1701 Posted July 13, 2006 Share Posted July 13, 2006 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.[code]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; } }[/code][b]however, even when I enter an endDate in the future, it triggers my alert().any thoughts?[/b] Quote Link to comment https://forums.phpfreaks.com/topic/14495-any-idea-why-this-validation-is-failing/ Share on other sites More sharing options...
janroald Posted July 13, 2006 Share Posted July 13, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/14495-any-idea-why-this-validation-is-failing/#findComment-57387 Share on other sites More sharing options...
micah1701 Posted July 13, 2006 Author Share Posted July 13, 2006 [quote]Your date comparison is all wrong. Say enddate is 2007/06/07 and startdate is 2006/05/06, then your comparison would beif(2006 <= 2007 && 06 <= 05 && 07 <= 06) { ^ ^Both month and day will return a false even though your date is in the future.[/quote]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 beif(2007 <= 2006 && June <= July && 5 <= 7) { pass fail failI'm still don't understand where my logic is flawed.[b]EDIT:[/b] I did, however work around this whole issue by just using the date object[code]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; } }[/code] Quote Link to comment https://forums.phpfreaks.com/topic/14495-any-idea-why-this-validation-is-failing/#findComment-57394 Share on other sites More sharing options...
janroald Posted July 14, 2006 Share Posted July 14, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/14495-any-idea-why-this-validation-is-failing/#findComment-57800 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.