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 February 7th, 2002, 05:42 AM
teknoart teknoart is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2002
Posts: 52 teknoart User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 57 m 25 sec
Reputation Power: 12
JavaScript thinks 1 > 10 ??

I have a control function like this
function control()
{
if(document.getElementById('valid_fromday').value==document.getElementById('valid_today').value && document.getElementById('valid_frommo').value==document.getElementById('valid_tomo').value && document.getElementById('valid_fromyear').value==document.getElementById('valid_toyear').value)
{
alert('ERROR! beginning and ending dates of a period must be different');
return false;
}
else if(document.getElementById('valid_fromday').value>= document.getElementById('valid_today').value && document.getElementById('valid_frommo').value>= document.getElementById('valid_tomo').value && document.getElementById('valid_fromyear').value>= document.getElementById('valid_toyear').value)
{
alert('ERROR! beginning date of the period can not be later then ending date');
return false;
} ...

the select lists are created using PHP like this
//DATE ARRAYS
$day=array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31);
$mo=array(0,Jan,Feb,Mar,Apr,Mai,Jun,Jul,Aug,Sep,Oct,Nov,Dec);
$thisyear=date("Y");
for($i=($thisyear-2);$i<($thisyear+5);$i++)
{
$year[]=$i;
}
...
From <select size="1" name="valid_fromday">
<?
for($i=1;$i<(count($day));$i++)
{
echo("<option value=\"$i\">$day[$i]</option>");
}
?>
</select> .... month year ext.

When the days or months between 1-9 selected as from date and
days and months greater 10 as to date, function alerts first date is later then second.
And when dates in the same month selected then it alerts dates must be different.
Why? and how can I solve the problem?

Reply With Quote
  #2  
Old February 7th, 2002, 05:57 AM
binky's Avatar
binky binky is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Oct 2001
Location: New Zealand
Posts: 1,774 binky User rank is Sergeant (500 - 2000 Reputation Level)binky User rank is Sergeant (500 - 2000 Reputation Level)binky User rank is Sergeant (500 - 2000 Reputation Level)binky User rank is Sergeant (500 - 2000 Reputation Level)binky User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 58 m 15 sec
Reputation Power: 23
Too tired, too much code to read so I'll have a guess. The values you are getting from the form inputs are strings. Use:

parseInt(document.getElement....)

to convert them to integers, then compare them.

Reply With Quote
  #3  
Old February 7th, 2002, 04:27 PM
teknoart teknoart is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2002
Posts: 52 teknoart User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 57 m 25 sec
Reputation Power: 12
thanks binky

but it doesn't work.
I have tried it with parseInt as you suggested, the control function couldn't detect the misentered dates. (The script has run errorless)

When I create the select lists with html coding, the control function works as expected. But when I create them using array in PHP as written in my first message they cause this problem. I am thinking it is not a PHP problem because I can let the date data store in db correctly and problem free.

Any help?

Reply With Quote
  #4  
Old February 8th, 2002, 03:05 AM
binky's Avatar
binky binky is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Oct 2001
Location: New Zealand
Posts: 1,774 binky User rank is Sergeant (500 - 2000 Reputation Level)binky User rank is Sergeant (500 - 2000 Reputation Level)binky User rank is Sergeant (500 - 2000 Reputation Level)binky User rank is Sergeant (500 - 2000 Reputation Level)binky User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 58 m 15 sec
Reputation Power: 23
Right then, more awake today. Firstly...

if(document.getElementById('valid_fromday').value>= document.getElementById('valid_today').value && document.getElementById('valid_frommo').value>= document.getElementById('valid_tomo').value && document.getElementById('valid_fromyear').value>= document.getElementById('valid_toyear').value)
{
alert('ERROR! beginning date of the period can not be later then ending date');
return false;
}

...this bit isn't correct. Not all of these have to be true to make the 'from' date later than the 'to' date. The day, month and year don't all have to be greater. If the day is less but the month is greater and the year the same, this statement wont return true.

What I would suggest is :

var fromDate = new Date(fromYear,fromMonth,fromDay);
var toDate = new Date(toYear,toMonth,toDay);
if (fromDate.getTime() > toDate.getTime()) {
alert('ERROR! beginning date of the period can not be later then ending date');
return false;
}

That would do a better comparison.

Reply With Quote
  #5  
Old February 8th, 2002, 05:36 PM
teknoart teknoart is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2002
Posts: 52 teknoart User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 57 m 25 sec
Reputation Power: 12
binky I need further help

binky,
I have asked about my problem also in PHP forum, and member merkinmuffley has replied as follows;
teknoart,

to get the value of the selected option from the select box, dont you have to go further than getting the .value attribute of the select box itself. the select box's value is not the same as the value of the selected option, because each option is also an object. they're stored in the .options array, and you can get the selected one by the .selectedIndex attribute...

mySelect.options[mySelect.selectedIndex].value

also I think you can shorten that to:

mySelect[mySelect.selectedIndex].value

but don't quote me on that one. anyway the point is that you might have to rewrite the conditionals to something like this:

var fromDay = document.getElementById('valid_fromday');
var toDay = document.getElementById('valid_today');

if (fromDay.options[fromDay.selectedIndex].value == toDay.options[toDay.selectedIndex].value && etc etc)

hth

/nick
----------------------
It was true, and what you suggest was also true, thatwhy I have modified my control function like this;
function control()
{
var fromDay = document.getElementById('valid_fromday');
var toDay = document.getElementById('valid_today');
var fromMonth = document.getElementById('valid_frommo');
var toMonth = document.getElementById('valid_tomo');
var fromYear = document.getElementById('valid_fromyear');
var toYear = document.getElementById('valid_toyear');
var fromDay=fromDay.options[fromDay.selectedIndex].value;
var toDay=toDay.options[toDay.selectedIndex].value;
var fromMonth=fromMonth.options[fromMonth.selectedIndex].value;
var toMonth=toMonth.options[toMonth.selectedIndex].value;
var fromYear=fromYear.options[fromYear.selectedIndex].value;
var toYear=toYear.options[toYear.selectedIndex].value;
var fromDate = new Date(fromYear,fromMonth,fromDay);
var toDate = new Date(toYear,toMonth,toDay);
if (fromDate.getTime() == toDate.getTime())
{
alert('ERROR! beginning and ending dates of a period must be different');
return false;
}
else if (fromDate.getTime() > toDate.getTime())
{
alert('ERROR! beginning date of the period can not be later then ending date');
return false;
}...
well done except year comparisions. Please help me further. Thanks a lot for your support

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > JavaScript thinks 1 > 10 ??

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