JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 2nd, 2007, 12:44 PM
zoopnfunk zoopnfunk is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2005
Posts: 61 zoopnfunk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 36 m 33 sec
Reputation Power: 8
HELP: Compare a date to a dropdown of dates

I am interested in adding a javascript confirmation to an button control I have on my asp.net page. The button conrol is called btnSubmit.

I have a calendar user control on my page called calNewProcessingDate. In javascript I can reference this control by using

document.getElementByID('calNewProcessingDate:txtDate').text

I also have a dropdown list called ddlProcessingDates

When my submit button is clicked I want to compare the date in the calNewProcessingDate control to all of the dates that are stored inside of the ddlProcessingDates dropdownlist.

If one of the dates in the ddlProcessingDates dropdown list matches the date in the calNewProcessingDate control I want to return a different message than if no matches are found.

So in pseudo-code what I am trying to do is the following:

Code:
If calNewProcessingDate IS IN ddlProcessingDates 
{
strReturnMessage = "Hello World"
}
else
{
strReturnMessage = "Good-bye Cruel World"
}

if (confirm(returnstring)==true) 
  {
    return true;
  }
  else 
  {
    return false;
  }


So far all I have is a weak javascript function called confirm_submit() that doesn't come close to doing the job. I've tinckered with it a little bit but as soon as I try to incorporate more than one date from the dropdownlist I run into trouble.

I need some help to finish writing this javascript function. Can anyone please help?


Here is a copy of the function as it currently stands:

Code:
function confirm_submit()
{

	var txtNewProcessingDate = document.getElementById('calNewProcessingDate:txtDate').text
	var newProcessingDate = new Date(txtNewProcessingDate)
	
	
	for (var i = 0; i < document.getElementById('ddlProcessingDates').count - 1; i++)
	{
		var txtRestoreProcessingDate = document.getElementById('ddlProcessingDates').Items(i).Value()
		var restoreProcessingDate = new Date(txtRestoreProcessingDate)
		
		if (restoreProcessingDate == newProcessingDate)
		{
			var isMatch = boolean(true)
		}
		else
		{
			var isMatch = boolean(false)
		}

	}
	
	
	if (isMatch) 
	{
		var returnstring = "hello world"
	}
	else 
	{
		var returnstring = "good-bye cruel world"
	}
	
  if (confirm(returnstring)==true) {
    return true;
    }
  else {
    return false;
    }
}

Last edited by zoopnfunk : November 2nd, 2007 at 01:01 PM.

Reply With Quote
  #2  
Old November 2nd, 2007, 01:56 PM
zoopnfunk zoopnfunk is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2005
Posts: 61 zoopnfunk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 36 m 33 sec
Reputation Power: 8
I'm getting a little closer but I still can't figure out why I am getting an 'undefined' value from my dropdown list. Here is my javascript:

Code:
function confirm_submit()
{

	var txtNewProcessingDate = document.getElementById('calNewProcessingDate:txtDate').value
	var newProcessingDate = new Date(txtNewProcessingDate)

	for (var i = 0; i < document.getElementById('ddlProcessingDates').options.length - 1; i++)
	{
	
		var txtRestoreProcessingDate = document.getElementById('ddlProcessingDates').options[i].Value
		var restoreProcessingDate = new Date(txtRestoreProcessingDate)

		alert("new processing date: " + newProcessingDate)
		alert("restore processing date: " + restoreProcessingDate)
		
		
 if (restoreProcessingDate == newProcessingDate)
		{
			var isMatch = boolean(true)
		}
		else
		{
			var isMatch = boolean(false)
		}

	}
	
	if (isMatch) 
	{
		var returnstring = "hello world"
	}
	else 
	{
		var returnstring = "good-bye cruel world"
	}
	
  if (confirm(returnstring)==true) {
    return true;
    }
  else {
    return false;
    }
}


if I add an alert to check any of the buckets of my dropdown list using document.getElementById('ddlProcessingDates').options[1].Value I get undefined no matter what value I place in the bucket. I'm not sure why... can anyone point me in the right direction?

Thanks!

Reply With Quote
  #3  
Old November 2nd, 2007, 02:21 PM
bals28mjk's Avatar
bals28mjk bals28mjk is offline
Person
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jan 2007
Location: Moon
Posts: 1,158 bals28mjk User rank is Colonel (50000 - 60000 Reputation Level)bals28mjk User rank is Colonel (50000 - 60000 Reputation Level)bals28mjk User rank is Colonel (50000 - 60000 Reputation Level)bals28mjk User rank is Colonel (50000 - 60000 Reputation Level)bals28mjk User rank is Colonel (50000 - 60000 Reputation Level)bals28mjk User rank is Colonel (50000 - 60000 Reputation Level)bals28mjk User rank is Colonel (50000 - 60000 Reputation Level)bals28mjk User rank is Colonel (50000 - 60000 Reputation Level)bals28mjk User rank is Colonel (50000 - 60000 Reputation Level)bals28mjk User rank is Colonel (50000 - 60000 Reputation Level)bals28mjk User rank is Colonel (50000 - 60000 Reputation Level)bals28mjk User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 6 h 1 m 45 sec
Reputation Power: 549
Value should be the lowercase value.
__________________
Give Opera some love. Use it and you'll see why.

Oh, those monkeys.::What's wrong with the world today?::The lion king in 5 seconds.

My body is so sexy.

Reply With Quote
  #4  
Old November 2nd, 2007, 02:32 PM
zoopnfunk zoopnfunk is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2005
Posts: 61 zoopnfunk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 36 m 33 sec
Reputation Power: 8
Quote:
Originally Posted by bals28mjk
Value should be the lowercase value.


Thanks, you were right about that so maybe u can help me out one more time. I am getting to the portion of my code where I check the two date values. However, even when the date values are equal (and display equal in my alert box) I am not getting my MATCH FOUND alert... so I am never actually detecting a match even when one exists...

Code:
function confirm_submit()
{

	var txtNewProcessingDate = document.getElementById('calNewProcessingDate:txtDate').value
	var newProcessingDate = new Date(txtNewProcessingDate)
	var isMatch = 0

	for (var i = 0; i < document.getElementById('ddlProcessingDates').options.length - 1; i++)
	{
		var txtRestoreProcessingDate = document.getElementById('ddlProcessingDates').options[i].value
		if (txtRestoreProcessingDate != -1)
		{
				var restoreProcessingDate = new Date(txtRestoreProcessingDate)
				alert("New Date: " + newProcessingDate)
				alert("Restore date: " + restoreProcessingDate)
			
				if (restoreProcessingDate == newProcessingDate)
				{
					alert("MATCH FOUND")
					isMatch = 1
				}
		}
	
	}
	
	if (isMatch == 1) 
	{
		var returnstring = "hello world"
	}
	else 
	{
		var returnstring = "good-bye cruel world"
	}
	
  if (confirm(returnstring)==true) 
	{
    return true;
  }
  else 
	{
    return false;
   }
}


The dates in the textbox and dropdown list are formatted mm/dd/yyyy but then I put them into the date object and the format changes -- either way they match and I am not entering my IF statement.

Thanks in advance!

Reply With Quote
  #5  
Old November 2nd, 2007, 08:30 PM
tom_cos's Avatar
tom_cos tom_cos is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Location: NJ, USA
Posts: 72 tom_cos User rank is Sergeant Major (2000 - 5000 Reputation Level)tom_cos User rank is Sergeant Major (2000 - 5000 Reputation Level)tom_cos User rank is Sergeant Major (2000 - 5000 Reputation Level)tom_cos User rank is Sergeant Major (2000 - 5000 Reputation Level)tom_cos User rank is Sergeant Major (2000 - 5000 Reputation Level)tom_cos User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 20 h 9 m 51 sec
Reputation Power: 48
Try This Code

I think you need to compare the value of the two variables. Here is some code which compares the value entered in newProcessingDate with the values in the option list ddlProcessingDates. When a match is found, the For loop is left (via break; command). Hope this does it for you!

Code:
	
	for (var i = 0; i < document.getElementById('ddlProcessingDates').options.length - 1; i++)
	{
		var txtRestoreProcessingDate = document.getElementById('ddlProcessingDates').options[i].value
		if (txtRestoreProcessingDate != -1)
		{
				var restoreProcessingDate = new Date(txtRestoreProcessingDate)
				alert("New Date: " + newProcessingDate + "\nRestore date: " + restoreProcessingDate)
			
				if (restoreProcessingDate.value == newProcessingDate.value)
				{
					alert("MATCH FOUND")
					isMatch = 1
					break;
				}
		}
	
	}
Comments on this post
zoopnfunk agrees: should be .valueOf() but a BIG Thank You for pointing me in the right direction!

Reply With Quote
  #6  
Old November 5th, 2007, 06:24 AM
zoopnfunk zoopnfunk is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2005
Posts: 61 zoopnfunk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 36 m 33 sec
Reputation Power: 8
Question

Quote:
Originally Posted by tom_cos
I think you need to compare the value of the two variables. Here is some code which compares the value entered in newProcessingDate with the values in the option list ddlProcessingDates. When a match is found, the For loop is left (via break; command). Hope this does it for you!

Code:
	
	for (var i = 0; i < document.getElementById('ddlProcessingDates').options.length - 1; i++)
	{
		var txtRestoreProcessingDate = document.getElementById('ddlProcessingDates').options[i].value
		if (txtRestoreProcessingDate != -1)
		{
				var restoreProcessingDate = new Date(txtRestoreProcessingDate)
				alert("New Date: " + newProcessingDate + "\nRestore date: " + restoreProcessingDate)
			
				if (restoreProcessingDate.value == newProcessingDate.value)
				{
					alert("MATCH FOUND")
					isMatch = 1
					break;
				}
		}
	
	}


Thanks for your input. I tried the above code and I am always returned MATCH FOUND. I added the .value to the alert and it appears that adding .value takes the date and makes it appear as 'undefined'. So I am always comparing undefined to undefined.

Any other suggestions? I'm really at a loss here. This shouldn't be such a difficult operation i'm just a serious jscript n00b.

So, ne1, bueller?

Reply With Quote
  #7  
Old November 5th, 2007, 06:50 AM
Winters Winters is offline
Super Moderator
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jul 2003
Posts: 3,925 Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 11 h 14 m 42 sec
Reputation Power: 2571
Can you post everything please, including the HTML.

Javascript Code:
Original - Javascript Code
  1. function confirm_submit() {
  2.   var newProcessingDate = new Date(document.getElementById('calNewProcessingDate:txtDate').text);
  3.  
  4.   for (var i = 0; i < document.getElementById('ddlProcessingDates').count - 1; i++) {
  5.     var txtRestoreProcessingDate = document.getElementById('ddlProcessingDates').Items(i).value();
  6.     var restoreProcessingDate = new Date(txtRestoreProcessingDate);
  7.  
  8.     if (restoreProcessingDate == newProcessingDate) var returnstring = "hello world";
  9.     else var returnstring = "good-bye cruel world";
  10.   }
  11.    
  12.   if (confirm(returnstring)) return true;
  13.   else return false;
  14. }
  15.  
__________________
[PHP] | [Perl] | [Python] | [Java] != [JavaScript] | [XML] | [ANSI C] | [C++] | [LUA] | [MySQL] | [FirebirdSQL] | [PostgreSQL] | [HTML] | [XHTML] | [CSS]

W3Fools - A W3Schools Intervention.

Reply With Quote
  #8  
Old November 5th, 2007, 07:15 AM
zoopnfunk zoopnfunk is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2005
Posts: 61 zoopnfunk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 36 m 33 sec
Reputation Power: 8
Ok.. so i've changed things up a little bit and have a slightly different issue. Now I believe I am always recieving the 'good-bye cruel world' message because the time doesn't match. Here is an example. I enter the date 08/30/2007 as the new processing date. This date also exists as 08/30/2007 in my dropdown list.

However, when I alert the dates they appear as follows:

NEW: Thu Aug 30 08:01:26 EDT 2007
DDL: Thu Aug 30 08:01:30 EDT 2007

I really just want to compare the dates, I don't care about the time down to the seconds.

Anyway, here is my javascript. If it is really neccesary to paste the entire HTML I will do so, but there are 350 lines of code on this page so I am hesitant to do that.


I am attaching my javascript function to a submit button using the following line of code:

Code:
Me.btnSubmit.Attributes.Add("onclick", "return confirm_submit();")


Here is the full javascript function:

Code:
function confirm_submit()
{

	var txtNewProcessingDate = document.getElementById('calNewProcessingDate:txtDate').value
	var tempNewProcessingDate = new Date(txtNewProcessingDate)
	
	var isMatch = 0
	
	var newProcessingDay = tempNewProcessingDate.getDate()
	var newProcessingMonth = tempNewProcessingDate.getMonth()
	var newProcessingYear = tempNewProcessingDate.getFullYear()
	
	var NewProcessingDate = new Date()
	NewProcessingDate.setFullYear(newProcessingYear,newProcessingMonth, newProcessingDay)
	
	for (var i = 0; i < document.getElementById('ddlProcessingDates').options.length - 1; i++)
	{
		var txtRestoreProcessingDate = document.getElementById('ddlProcessingDates').options[i].value
		if (txtRestoreProcessingDate != -1)
		{
				var tempRestoreProcessingDate = new Date(txtRestoreProcessingDate)
				
				var restoreProcessingDay = tempRestoreProcessingDate.getDate()
				var restoreProcessingMonth = tempRestoreProcessingDate.getMonth()
				var restoreProcessingYear = tempRestoreProcessingDate.getFullYear()
				
				var restoreProcessingDate = new Date()
				restoreProcessingDate.setFullYear(restoreProcessingYear, restoreProcessingMonth, restoreProcessingDay)
				
				
				alert("New Date: " + txtNewProcessingDate + "\nRestore date: " + txtRestoreProcessingDate)
				alert("New Date: " + NewProcessingDate + "\nRestore date: " + restoreProcessingDate)
			
				if (NewProcessingDate == restoreProcessingDate)
				{
					alert("MATCH FOUND")
					isMatch = 1
					break;
				}
		}
	
	}

	
	if (isMatch == 1) 
	{
		var returnstring = "hello world"
	}
	else 
	{
		var returnstring = "good-bye cruel world"
	}
	
  if (confirm(returnstring)==true) 
	{
    return true;
  }
  else 
	{
    return false;
   }
}


Of course, this is inside of the <head> and <script language="javascript"> tags.

I also have a second issue -- it appears that some dates in the dropdown list are EST and others are EDT. Is there any way I can make sure these are consistent?

Reply With Quote
  #9  
Old November 5th, 2007, 07:22 AM
zoopnfunk zoopnfunk is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2005
Posts: 61 zoopnfunk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 36 m 33 sec
Reputation Power: 8
Ok, I eliminated the time issue by removing the call to setFullYear and establishing my dates like so:

var NewProcessingDate = new Date(newProcessingYear,newProcessingMonth,newProcessingDay)


However, even when there is a match I am not entering my ALERT for MATCH FOUND. The dates appear in the alert and match exactly yet they are not evaluating correctly... i'm perplexed...

here is my function:

Code:
function confirm_submit()
{

	var txtNewProcessingDate = document.getElementById('calNewProcessingDate:txtDate').value
	var tempNewProcessingDate = new Date(txtNewProcessingDate)
	
	var isMatch = 0
	
	var newProcessingDay = tempNewProcessingDate.getDate()
	var newProcessingMonth = tempNewProcessingDate.getMonth()
	var newProcessingYear = tempNewProcessingDate.getFullYear()
	
	var NewProcessingDate = new Date(newProcessingYear,newProcessingMonth,newProcessingDay)
	
	for (var i = 0; i < document.getElementById('ddlProcessingDates').options.length - 1; i++)
	{
		var txtRestoreProcessingDate = document.getElementById('ddlProcessingDates').options[i].value
		if (txtRestoreProcessingDate != -1)
		{
				var tempRestoreProcessingDate = new Date(txtRestoreProcessingDate)
				
				var restoreProcessingDay = tempRestoreProcessingDate.getDate()
				var restoreProcessingMonth = tempRestoreProcessingDate.getMonth()
				var restoreProcessingYear = tempRestoreProcessingDate.getFullYear()
				
				var restoreProcessingDate = new Date(restoreProcessingYear, restoreProcessingMonth, restoreProcessingDay)
					
				alert("New Date: " + txtNewProcessingDate + "\nRestore date: " + txtRestoreProcessingDate)
				alert("New Date: " + NewProcessingDate + "\nRestore date: " + restoreProcessingDate)
			
				if (NewProcessingDate == restoreProcessingDate)
				{
					alert("MATCH FOUND")
					isMatch = 1
					break;
				}
		}
	
	}

	
	if (isMatch == 1) 
	{
		var returnstring = "hello world"
	}
	else 
	{
		var returnstring = "good-bye cruel world"
	}
	
  if (confirm(returnstring)==true) 
	{
    return true;
  }
  else 
	{
    return false;
   }
}


Any suggestions appreciated .

Reply With Quote
  #10  
Old November 5th, 2007, 08:04 AM
zoopnfunk zoopnfunk is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2005
Posts: 61 zoopnfunk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 36 m 33 sec
Reputation Power: 8
Talking Got it

I'm a moron. The helpful person above had it right with the .Value -- except it should be .valueOf() (I was also missing the parens.. do'h).

Anyway, when I compare the values and not just the objects I am getting the results I expect.

Thanks for all of your help!

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > HELP: Compare a date to a dropdown of dates

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap