6 Replies - 4218 Views - Last Post: 15 December 2011 - 07:39 PM

#1 newbieHQ  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 20-January 11

JavaScript onchange function on drop down list

Posted 15 December 2011 - 12:23 AM

hi ..everyone.. i am trying to validate the option drop down list of date which consists day, month and year..
lets say if the user choose 31 on 'day', February on 'month' and 2011 for 'year' .. it will return alert error ..
i have been trying to validate and display the error if the user choose the improper date like the example above ..

below is my script for the validation date... and it is quite easier to understand as well... :D

hope someone here can help me ... thank you ... ;)

<html>
<head>

<script type="text/javascript">
  <!--

function calcDate(frmDate)
   {
   //checks for a valid date
   if (validateDate(frmDate.cboday, frmDate.cbomonth, frmDate.cboyear) == false)
      {
      frmDate.txtdate.value = "";
      alert("Invalid Date" + "\n" + "Please Check!");
      }
   else
      {
      //if all 3 dropdowns aren't "- -" then populate txtdate textbox
      if (frmDate.cboday.selectedIndex != 0 && frmDate.cbomonth.selectedIndex != 0
        && frmDate.cboyear.selectedIndex != 0)
         {
         frmDate.txtdate.value = frmDate.cboday.value + "/" + 
           frmDate.cbomonth.value + "/" + frmDate.cboyear.value;
         }
      else
         {
         frmDate.txtdate.value = "";
         }
      }
   }		

function validateDate(day, month, year)
   //check correct number of day for given month/year
   {
   if (day.selectedIndex != 0 && month.selectedIndex != 0 
     && year.selectedIndex != 0)
      {
      switch(month.value) 
         {
         case "02" :
            //February
            if (year.value == Math.round(year.value / 4) * 4)
	       //leap year
	       {
	       if (day.value > 29)
                  {
                  return false;
                  }
               }
            else
               {
               //non-leap year
               if (day.value > 28)
                  {
                  return false;
                  }
               }
               break;	
         case "04" :
            //April
            if (day.value > 30)
               {
               return false;
               }	
               break;
         case "06":
            //June
            if (day.value > 30)
               {
               return false;
               }	
               break;		
         case "09":
            //September
            if (day.value > 30)
               {
               return false;
               }	
               break;
         case "11":
            //November
            if (day.value > 30)
               {
               return false;
               }	
               break;
         default:
            //date is valid
            return true;
            break;
         }
      }
   }	

  // -->
</SCRIPT>

</head>
<body>
<form name="frmDate">
<select name="cboDay" onchange="calcDate(frmDate);">
<option selected>- -</option>
<%
'populate day dropdown
For intCount = 1 to 31
   response.write "<Option value=""" & intCount & """>" & _
     intCount & "</option>"
Next
%>
</select>

<select name="cbomonth" onchange="calcDate(frmDate);">
'populate month dropdown
<option selected>- -</option>
<Option value="01">January</option>
<Option value="02">February</option>
<Option value="03">March</option>
<Option value="04">April</option>
<Option value="05">May</option>
<Option value="06">June</option>
<Option value="07">July</option>
<Option value="08">August</option>
<Option value="09">September</option>
<Option value="10">October</option>
<Option value="11">November</option>
<Option value="12">December</option>
</select>

<!--#INCLUDE FILE="ConvDate.inc"-->

<select name="cboyear" onchange="calcDate(frmDate);">
<option selected>- -</option>

<%
'populate year dropdown
intYear = ConvDate(Date, "%Y")

for intCount = (intYear - 10) to (intYear + 10)
	response.write "<Option value=""" & intCount & """>" & _
          intCount & "</option>"
next
%>
</select>
<br>
<input type="text" name="txtdate">
</form>
</body>
</html>



for the ConvDate.inc part
   intPosItem = Instr(strFormat, "%Y")
   Do While intPosItem > 0
      strFormat = Left(strFormat, intPosItem-1) & _
      DatePart("yyyy",strDate) & _
      Right(strFormat, Len(strFormat) - (intPosItem + 1))
      intPosItem = Instr(strFormat, "%Y")
   Loop



thanks once again !

This post has been edited by Dormilich: 15 December 2011 - 04:12 AM
Reason for edit:: fixed code tag


Is This A Good Question/Topic? 0
  • +

Replies To: JavaScript onchange function on drop down list

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,465
  • Joined: 27-December 08

Re: JavaScript onchange function on drop down list

Posted 15 December 2011 - 12:25 AM

Moved to Javascript. Please note that Java != Javascript.
Was This Post Helpful? 0
  • +
  • -

#3 newbieHQ  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 20-January 11

Re: JavaScript onchange function on drop down list

Posted 15 December 2011 - 12:31 AM

sorry ..my bad ...
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: JavaScript onchange function on drop down list

Posted 15 December 2011 - 04:11 PM

Why do you seem to be mixing Javascript and VBScript? Are you engaging in blind copy/paste programming, or are you intentionally doing this?
Was This Post Helpful? 0
  • +
  • -

#5 newbieHQ  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 20-January 11

Re: JavaScript onchange function on drop down list

Posted 15 December 2011 - 07:10 PM

no .. i am not! the javascript for the above program is used to identify whether the date the user choose is correct or not... by using onchange function ..but for some reason ..it cant run and cant capture the date the user choose like the error displayed when user choose 30 february 2011 .. (obviously 30 february does not exist in the calendar) so i am wondering is there any coding error on the on change function or other...

thanks once again !
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,465
  • Joined: 27-December 08

Re: JavaScript onchange function on drop down list

Posted 15 December 2011 - 07:30 PM

This code is not Javascript. It can be found verbatim here. Why are you using VBScript?
intPosItem = Instr(strFormat, "%Y")
Do While intPosItem > 0
   strFormat = Left(strFormat, intPosItem-1) & _
   DatePart("yyyy",strDate) & _
   Right(strFormat, Len(strFormat) - (intPosItem + 1))
   intPosItem = Instr(strFormat, "%Y")
Loop


Was This Post Helpful? 0
  • +
  • -

#7 newbieHQ  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 20-January 11

Re: JavaScript onchange function on drop down list

Posted 15 December 2011 - 07:39 PM

that wan is used to populate the year displayed

<!--#INCLUDE FILE="ConvDate.inc"-->
<%
'populate year dropdown
intYear = ConvDate(Date, "%Y")

for intCount = (intYear - 10) to (intYear + 10)
	response.write "<Option value=""" & intCount & """>" & _
          intCount & "</option>"
next
%>



it is from other page which is called ConvDate.inc ...

the reason i using vbscript is because the main programming language for server side is using vbscript and use to develop a program for the outside organization .

i am nor provided using asp.net because it is expensive enough for me to purchase the software. :D

This post has been edited by Dormilich: 16 December 2011 - 03:49 AM
Reason for edit:: fixed code tag

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1