473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Date_2 always higher Date_1 in the selectIndex

Hello everyone.

I have this html page:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2.  
  3. <head>
  4.  
  5. </head>
  6.  
  7. <body>
  8.  
  9. <select size="1" name="date_1" onChange="window.document.location='Tot.asp?date_2=24/04/2008&date_1='+this.options[this.selectedIndex].value;">
  10. <option>select</option>
  11. <option value="11/03/2008">11/03/2008</option>
  12. <option value="12/03/2008">12/03/2008</option>
  13. <option value="13/03/2008">13/03/2008</option>
  14. <option value="14/03/2008">14/03/2008</option>
  15. <option value="17/03/2008">17/03/2008</option>
  16.  
  17. </select>
  18.  
  19. <select size="1" name="date_2" onChange="window.document.location='Tot.asp?date_1=24/04/2008&date_2='+this.options[this.selectedIndex].value;">
  20. <option>select</option> 
  21. <option value="11/03/2008">11/03/2008</option>
  22. <option value="12/03/2008">12/03/2008</option>
  23. <option value="13/03/2008">13/03/2008</option>
  24. <option value="14/03/2008">14/03/2008</option>
  25. <option value="17/03/2008">17/03/2008</option>
  26. </select>
  27.  
  28.  
  29. </body>
  30.  
  31. </html>
  32.  
I would like javascript function check that:

date_1 = 24/04/2008
date_2 = 25/04/2008

it's right....

date_1 = 25/04/2008
date_2 = 24/04/2008

it's wrong....

Check that date_2 always higher date_1, it's possible?
Apr 26 '08 #1
24 1532
acoder
16,027 Expert Mod 8TB
You're setting document.location. This is deprecated in favour of window.location.href.

Do you want to make the check onchange? You'll have to call a function to make the check before you change the URL unless you're planning to make the check on the server-side.

To make sure Date_2 is always higher than Date1 , make two Date objects, set the dates using the setFullYear method and then make a simple comparison:
Expand|Select|Wrap|Line Numbers
  1. if (date1 > date2) //alert...
Date Object reference
Apr 26 '08 #2
Hi acoder.

thanks for your reply, but i dont understand your suggestion :

[php]

<html>

<head>


<SCRIPT LANGUAGE=Javascript>

var date_1, date_2;

if ( date_1 > date_2 )
alert( 'Stop !.' );


</SCRIPT>

</head>

<body>

<select size="1" name="date_1" onChange="window.location.href='Tot.asp?date_2=24/04/2008&date_1='+this.options[this.selectedIndex].value;">
<option>select</option>
<option value="11/03/2008">11/03/2008</option>
<option value="12/03/2008">12/03/2008</option>
<option value="13/03/2008">13/03/2008</option>
<option value="14/03/2008">14/03/2008</option>
<option value="17/03/2008">17/03/2008</option>

</select>

<select size="1" name="date_2" onChange="window.location.href='Tot.asp?date_1=24/04/2008&date_2='+this.options[this.selectedIndex].value;">
<option>select</option>
<option value="11/03/2008">11/03/2008</option>
<option value="12/03/2008">12/03/2008</option>
<option value="13/03/2008">13/03/2008</option>
<option value="14/03/2008">14/03/2008</option>
<option value="17/03/2008">17/03/2008</option>
</select>


</body>

</html>

[/php]

Not working... can you tell me where the point when I was wrong ?
Apr 26 '08 #3
acoder
16,027 Expert Mod 8TB
No, it doesn't work like that.

Put the code into a function and call it, e.g.
Expand|Select|Wrap|Line Numbers
  1. function dateValidate() {
  2. // code goes here
  3. }
See how to use the Date object here.

When do you want to call this code? When you press the submit button?
Apr 26 '08 #4
No, it doesn't work like that.

Put the code into a function and call it, e.g.
Expand|Select|Wrap|Line Numbers
  1. function dateValidate() {
  2. // code goes here
  3. }
See how to use the Date object here.

When do you want to call this code? When you press the submit button?
OK, but this page is not a form...nothing submit button, only onchange event...
Apr 26 '08 #5
acoder
16,027 Expert Mod 8TB
OK, but this page is not a form...nothing submit button, only onchange event...
So call the function in the onchange event handler. It might be a good idea to move the location.href into the function too. If it validates, send the user off to the next page. If it doesn't, alert the error.
Apr 26 '08 #6
One example, please acoder... I am newbie...
Apr 26 '08 #7
acoder
16,027 Expert Mod 8TB
OK, have a look at this example:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript"> 
  2. function validateDate() {
  3.   var date1, date2, date_1, date_2;
  4.   // get date1 value
  5.   date1 = document.getElementById("date1").value;
  6.   // split the values to get the date/month/year
  7.   date1 = date1.split("/");
  8.   //create new date object
  9.   date_1 = new Date();
  10.   // set the date - see ref
  11.   date_1.setFullYear(date1[2],date1[1]-1,date1[0]);
  12.   // ...
  13.   // repeat for date2...
  14.   //...
  15.   // now compare
  16.   if ( date_1 > date_2 ) {
  17.     alert( 'Stop !.' );
  18.   else {
  19.     window.location.href = 'Tot.asp?date_1=' + encodeURIComponent(date1.join("/")) + '&date_2=' + encodeURIComponent(date2.join("/"));
  20.   }
  21. }
  22.  
For the select elements, add ids and change the onchange to call the function:
Expand|Select|Wrap|Line Numbers
  1. <select id="date1" size="1" name="date_1" onchange="validateDate()">
Apr 27 '08 #8
Thanks x your reply, but not working... the link:

http://www11.asphost4free.com/Miguel61/
Apr 27 '08 #9
acoder
16,027 Expert Mod 8TB
You'll also have to add a check that the date1 and date2 values are not empty (the first option Seleziona) :
Expand|Select|Wrap|Line Numbers
  1. if (date1.value == "") return;
Add a value attribute for the first option:
Expand|Select|Wrap|Line Numbers
  1. <select ...>
  2. <option value="">Seleziona</option>
Apr 28 '08 #10
You'll also have to add a check that the date1 and date2 values are not empty (the first option Seleziona) :
Expand|Select|Wrap|Line Numbers
  1. if (date1.value == "") return;
Add a value attribute for the first option:
Expand|Select|Wrap|Line Numbers
  1. <select ...>
  2. <option value="">Seleziona</option>
Sorry... I can not... :

[php]
code:<HTML>
<HEAD>
<TITLE>2007</TITLE>

<script type="text/javascript">

function validateDate()

{
var date1, date2, date_1, date_2;
// get date1 value
date1 = document.getElementById("date1").value;
// split the values to get the date/month/year
date1 = date1.split("/");
//create new date object
date_1 = new Date();
// set the date - see ref
date_1.setFullYear(date1[2],date1[1]-1,date1[0]);

// repeat for date2...
// get date2 value
date2 = document.getElementById("date2").value;
// split the values to get the date/month/year
date2 = date2.split("/");
//create new date object
date_2 = new Date();
// set the date - see ref
date_2.setFullYear(date2[2],date2[1]-1,date2[0]);

//...
// now compare

if (date1.value == "") return;

if ( date_1 > date_2 )

{
alert ("Stop");
}

else

{
window.location.href = 'index.htm?date_1=' + encodeURIComponent(date1.join("/")) + '&date_2=' + encodeURIComponent(date2.join("/"));


}
}

</script>
</HEAD>
<BODY>

<select id="date1" name="date_1" onchange="validateDate()">
<option value="">Seleziona</option>
<option value="19/07/2007">19/08/2007</option>
<option value="19/08/2007">19/09/2007</option>
<option value="19/09/2007">19/10/2007</option>
</select>

<select id="date2" name="date_2" onchange="validateDate()">
<option value="">Seleziona</option>
<option value="29/07/2007">29/05/2007</option>
<option value="29/08/2007">29/06/2007</option>
<option value="29/09/2007">29/07/2007</option>
</select>

</BODY>
</HTML>

[/php]
Apr 28 '08 #11
acoder
16,027 Expert Mod 8TB
You didn't add the javascript:
Expand|Select|Wrap|Line Numbers
  1.   // get date1 value
  2.   date1 = document.getElementById("date1").value;
  3.   if (date1 == "") return;
  4.  
Apr 28 '08 #12
You didn't add the javascript:
Expand|Select|Wrap|Line Numbers
  1.   // get date1 value
  2.   date1 = document.getElementById("date1").value;
  3.   if (date1 == "") return;
  4.  
thanks, but:

Object doesn't support this property or method
in the line:

[php]
window.location.href = 'index.htm?date_1=' + encodeURIComponent date1.join("/")) + '&date_2=' + encodeURIComponent(date2.join("/"));
[/php]
Apr 28 '08 #13
acoder
16,027 Expert Mod 8TB
The opening bracket went missing:
Expand|Select|Wrap|Line Numbers
  1. window.location.href = 'index.htm?date_1=' + encodeURIComponent(date1.join("/")) + '&date_2=' + encodeURIComponent(date2.join("/"));
  2.  
Apr 28 '08 #14
No... the same error...

[php]
code:<HTML>
<HEAD>
<TITLE>2007</TITLE>

<script type="text/javascript">

function validateDate()

{
var date1, date2, date_1, date_2;
// get date1 value
date1 = document.getElementById("date1").value;
// split the values to get the date/month/year
date1 = date1.split("/");
//create new date object
date_1 = new Date();
// set the date - see ref
date_1.setFullYear(date1[2],date1[1]-1,date1[0]);

// repeat for date2...
// get date2 value
date2 = document.getElementById("date2").value;
// split the values to get the date/month/year
date2 = date2.split("/");
//create new date object
date_2 = new Date();
// set the date - see ref
date_2.setFullYear(date2[2],date2[1]-1,date2[0]);

//...
// now compare

date1 = document.getElementById("date1").value;
if (date1.value == "") return;

if ( date_1 > date_2 )

{
alert ("Stop");
}

else

{
//window.location.href = 'index.htm?date_1=' + encodeURIComponent(date1.join("/")) + '&date_2=' + encodeURIComponent(date2.join("/"));
window.location.href = 'index.htm?date_1=' + encodeURIComponent(date1.join("/")) + '&date_2=' + encodeURIComponent(date2.join("/"));

}
}

</script>
</HEAD>
<BODY>

<select id="date1" name="date_1" onchange="validateDate()">
<option value="">Seleziona</option>
<option value="19/07/2007">19/08/2007</option>
<option value="19/08/2007">19/09/2007</option>
<option value="19/09/2007">19/10/2007</option>
</select>

<select id="date2" name="date_2" onchange="validateDate()">
<option value="">Seleziona</option>
<option value="29/07/2007">29/05/2007</option>
<option value="29/08/2007">29/06/2007</option>
<option value="29/09/2007">29/07/2007</option>
</select>

</BODY>
</HTML>

[/php]
Apr 28 '08 #15
acoder
16,027 Expert Mod 8TB
Remove line 33.

Line 34 should go where line 13 is, i.e. after line 12. Do you understand where you went wrong? join() is a method of an Array object and you made date1 into a string.

Repeat the empty string check for date2 too.
Apr 28 '08 #16
Remove line 33.

Line 34 should go where line 13 is, i.e. after line 12. Do you understand where you went wrong? join() is a method of an Array object and you made date1 into a string.

Repeat the empty string check for date2 too.

No, not understand.... :-(
Apr 28 '08 #17
acoder
16,027 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. function validateDate() 
  2.  
  3. {
  4.   var date1, date2, date_1, date_2;
  5.   // get date1 value
  6.   date1 = document.getElementById("date1").value;
  7.   if (date1 == "") return;
  8.   // split the values to get the date/month/year
  9.   date1 = date1.split("/");
  10.   //create new date object
  11.   date_1 = new Date();
  12.   // set the date - see ref
  13.   date_1.setFullYear(date1[2],date1[1]-1,date1[0]);
  14.  
  15.   // repeat for date2...
  16.   // get date2 value
  17.   date2 = document.getElementById("date2").value;
  18.   if (date2 == "") return;
  19.   // split the values to get the date/month/year
  20.   date2 = date2.split("/");
  21.   //create new date object
  22.   date_2 = new Date();
  23.   // set the date - see ref
  24.   date_2.setFullYear(date2[2],date2[1]-1,date2[0]);
  25.  
  26.   //...
  27.   // now compare  
  28.   if ( date_1 > date_2 ) 
  29.  
  30.   {
  31.     alert ("Stop");
  32.   }
  33.  
  34.   else 
  35.  
  36.   {
  37.       window.location.href = 'index.htm?date_1=' + encodeURIComponent(date1.join("/")) + '&date_2=' + encodeURIComponent(date2.join("/"));      
  38.  
  39.  }
  40. }
Apr 28 '08 #18
acoder
16,027 Expert Mod 8TB
Your option values don't match the text for each option, e.g.
[html]<option value="19/07/2007">19/08/2007</option>[/html]
Apr 29 '08 #20
Your option values don't match the text for each option, e.g.
[html]<option value="19/07/2007">19/08/2007</option>[/html]
thanks, I am distracted and a disaster... :-)
Apr 29 '08 #21
acoder
16,027 Expert Mod 8TB
So does that mean it's all working fine now?
Apr 29 '08 #22
So does that mean it's all working fine now?
Yes, your script working well, very well...
thanks !
Apr 29 '08 #23
acoder
16,027 Expert Mod 8TB
You're welcome.

One final point: if you understand the script, you can remove all the extra comments (lines starting with //).
Apr 29 '08 #24
You're welcome.

One final point: if you understand the script, you can remove all the extra comments (lines starting with //).
OK...... !!!
Regards,
Apr 29 '08 #25

Sign in to post your reply or Sign up for a free account.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
0
by: | last post by:
Hello All, Simple question, how do you tell the DataGrid.selectIndex to return the value in column 2?
354
by: Montrose... | last post by:
After working in c# for a year, the only conclusion I can come to is that I wish I knew c. All I need is Linux, the gnu c compiler and I can do anything. Web services are just open sockets...
22
by: Fred Ma | last post by:
I'm using the expression "int a = ceil( SomeDouble )". The man page says that ceil returns the smallest integer that is not less than SomeDouble, represented as a double. However, my...
26
by: Jeff_Relf | last post by:
Hi Olaf_Baeyens ( and Linonut ), Microsoft C++ is really it's latest version of MS_C, as Microsoft is not supporting the latest C standard, C99. cout and the STL are pure garbage and should...
4
by: Amjad | last post by:
I was looking at the MSCOMM32.OCX help when I found that it supports only up to 256000 bps baud rate. Is there another .NET COM that supports higher baud rates (e.g. 460800 and 921600 bps) Thanks...
8
by: arganx | last post by:
The conditional statement "if(j.... always executes regardless of what you enter. BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam) { char buffer=""; GetWindowText(hwnd,buffer,256);...
2
by: barry | last post by:
I am using the animation extender in asp.net ajax and have a RadioButtonList in a div and access it in javascript through the extender. The object passed into the javascript function is...
3
by: Kaheru | last post by:
Hi, my IT knowledge not that strong. Hope you guys dun mind I asking a stupid question. I recently been assign with an assignment to performance test a FTP server. I gathered the data using a test...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.