www.webdeveloper.com
Results 1 to 5 of 5

Thread: Comparing two dates on a form

  1. #1
    Join Date
    Nov 2002
    Location
    Norwood, MA
    Posts
    187

    Comparing two dates on a form

    Aloha!

    I've been borrowing JS for over 7 years to use in an application I developed for work, and still to this day I have no clue what I'm doing. I'm hopeful that a kind JS veteran developer can help me out!

    We have a form set up to record periods of days between two dates, and I just want to add JS to verify that the beginning date is not equal to or later than the end date (otherwise the days count would be 0 or negative, which would be nonsensical).

    The trick is that the code I thought would work, below, is yielding in IE8 (the ONLY browser my company allows) an error of "Object doesn't support this property or method". Here's the JS code for the date comparison:

    Code:
    function dateCompare() {
    	var EndDate = document.getElementByID('End_Date').value ;
    	var BeginDate = document.getElementByID('Begin_Date').value ;
    	alert ('BeginDate = ' + BeginDate + ' - EndDate = ' + EndDate) ;
    	return false;
    	if (EndDate = "")  {
    		if (EndDate <= BeginDate)  {
    			alert('The end date cannot be the same or earlier as the begin date');
    			return false;
    		}
    	}
    }
    I tried pulling the field value using document.forms.DateSelector.End_Date.value and if I do that, the code complains that "document.forms.DateSelector.End_Date' is null or not an object".

    Perhaps, I'm theorizing, the problem is due to the use of the handy dandy calendar picker tool developed by Mihai Bazon and explained here: http://www.dynarch.com/projects/calendar/old/

    If anyone has ideas on what I could try to pull the date value from each field and compare the two to verify they are in the right order in time, I'd be very appreciative! I searched and found a huge amount of examples, and tried several, but the issue keeps coming up. I think it's all about the form field values not pulling up.

    Thanks very much in advance for the help!

    Me

  2. #2
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    1,033
    Well first things first, 'getElementByID()' is not a valid javascript method. So if this isn't a typo, you will need to adjust those lines to use 'getElementById()' instead.

  3. #3
    Join Date
    Nov 2002
    Location
    Norwood, MA
    Posts
    187
    Thank you! See what I meant about still not having a clue after 7 years of "writing" JS into my application? Anyway, I found the following code to work, although it's probably less elegant than it could be. Any feedback to make it nicer?

    Code:
    function dateCompare() {
    	var EndDate = Date.parse(document.getElementById('End_Date').value) ;
    	var BeginDate = Date.parse(document.getElementById('Begin_Date').value) ;
    	alert ('BeginDate = ' + BeginDate + ' - EndDate = ' + EndDate) ;
    	if (isNaN(EndDate))
    		{
    		}
    	else
    		{
    		if (EndDate <= BeginDate)  {
    			alert('The end date cannot be the same or earlier as the begin date');
    			return false;
    		}
    	}
    }
    Thanks again, really appreciate it!!!

    Me

  4. #4
    Join Date
    Mar 2007
    Posts
    4
    change the above code
    if (!isNaN(EndDate)){

    if (EndDate <= BeginDate) {
    alert('The end date cannot be the same or earlier as the begin date');
    return false;
    }

    }

  5. #5
    Join Date
    Nov 2002
    Location
    Norwood, MA
    Posts
    187
    Thank you very much!!! I forgot that I knew that ! meant "not".

    Lovely, thanks!

    Me

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles