DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Month, Date, And Year Drop-Downs For ASP Pages

08.12.2009
| 1635 views |
  • submit to reddit
        If you need month, date, and year select boxes in your ASP, this code will save you some typing. Included is a sample ASP page that makes use of the functions that construct the drop downs. If you want to run the sample, paste all the code into a text editor and save the file as testdate.asp on a web server.

<%
Function MonthDropDown
Response.Write "<SELECT NAME = 'Month'>"
Response.Write "<OPTION VALUE = '01'>01</OPTION>"
Response.Write "<OPTION VALUE = '02'>02</OPTION>"
Response.Write "<OPTION VALUE = '03'>03</OPTION>"
Response.Write "<OPTION VALUE = '04'>04</OPTION>"
Response.Write "<OPTION VALUE = '05'>05</OPTION>"
Response.Write "<OPTION VALUE = '06'>06</OPTION>"
Response.Write "<OPTION VALUE = '07'>07</OPTION>"
Response.Write "<OPTION VALUE = '08'>08</OPTION>"
Response.Write "<OPTION VALUE = '09'>09</OPTION>"
Response.Write "<OPTION VALUE = '10'>10</OPTION>"
Response.Write "<OPTION VALUE = '11'>11</OPTION>"
Response.Write "<OPTION VALUE = '12'>12</OPTION>"
Response.Write "</SELECT>"
End Function

Function DayDropDown(MaxDay)
'Pass empty string if you 
'want to use default value
'of 31 for MaxDay

if not isNumeric(MaxDay) then
	MaxDay = 31
elseif cint(MaxDay) < 1 or (MaxDay) > 31 then
        MaxDay = 31
elseif MaxDay < 28 then 'No month has < 28 days, but
                        'change if you want to use for
                        'other purposes
    MaxDay = 28
end if

Response.Write "<SELECT NAME = 'Day'>"
For iCtr = 1 to MaxDay
      Response.Write "<OPTION VALUE = '" & iCtr & "'>"
      Response.Write iCtr & "</OPTION>" & vbCrlf
Next      
Response.Write "</SELECT>"
End Function


Function YearDropDown(BeginYear, EndYear)
'Pass "" for parameters
'if you want default values of
'1900 and current year

If Not isNumeric(BeginYear) then
     BeginYear = 1900
elseif BeginYear > Year(Now) or cint(BeginYear) < 0 then
     BeginYear = 1900
else
     BeginYear = cint(BeginYear)
End if

If Not isNumeric(EndYear) then
     EndYear = Year(Now)
elseif EndYear < BeginYear then
     EndYear = Year(Now)
else
     EndYear = Cint(EndYear)
end if

Response.Write "<SELECT NAME = 'Year'>"
For iCtr = BeginYear to EndYear
       Response.Write "<OPTION VALUE = '" & iCtr & "'>"
       Response.Write iCtr & "</OPTION>" & vbCrlf
Next      
Response.Write "</SELECT>"

End Function

'BELOW IS JUST SAMPLE HTML/ASP THAT USES THE 
'ABOVE FUNCTIONS
%>
<HTML>
<BODY>

<% if request("cmdSubmit") = "" then %>
<CENTER><B>SELECT A DATE</B>
<FORM NAME = "DateExample" ACTION = "TestDate.asp">
<TABLE CELLSPACING = 5>
<TR>
<TD><B>Month:</TD><TD><%MonthDropDown%></TD>
<TD><B>Day:</TD><TD><%DayDropDown "" %></TD>
<TD><B>Year:</TD><TD><%YearDropDown "", ""%></TD>
</TR>
</TABLE><P>
<INPUT TYPE = "SUBMIT" NAME = "cmdSubmit" Value = "Submit">
</TD><TR>
</FORM>
</CENTER>

<%
else

sDate = Request("Month") & "/" & Request("Day") & "/" & Request("Year")
  if isDate(sDate) then %>
      <B>You've entered a valid date!</B>
  <% else %>
  <B>You've entered a invalid date!</B>
  <% end if %>
 <P><A HREF = 'testdate.asp'>Enter another date</A>

<% end if %>

</BODY>
</HTML>