Jump to content

any idea why this validation is failing?


micah1701

Recommended Posts

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]
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

[quote]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.[/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 be

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

I'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]
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.