Dynamic Date Drop-down Boxes

By Kevin Perkins

Article

Article:

One thing I've learned over the last few years is to use techniques in my applications that make it easy to debug and maintain, especially if you leave the project and have to come back to it at a later time.

Besides error handling and commenting, I try to automate as much as possible to make maintenance a cake-walk. One area that can be tremendously automated is implementing date structures in your application.

For instance... if you're building an ecommerce application, you'll eventually have to deal with credit card expiry. The predominant technique for handling expiration dates is to use two drop-down boxes representing the month and the year. Having said that, it's very easy to hard-code those dates in each drop-down box.... 1-12 for month, but what to what for the year? Do you use the current year (2000) and the following two (2001,2002) ?

What happens when we're in year 2001? You'll have an older entry for the previous year (2000), and you'll be one short for the future (2002,?). This is bad practice because your application should be smart enough to remove previous years that are invalid to present-day transactions, as well as account for new credit cards that might have an expiration date farther in the future.

To work with drop-down dates dynamically, I determine the current month, current year, and how far into the future I want to account for expiration. My rule is 5 years, but each application is different:

<%@LANGUAGE=VBSCRIPT%>

<%
' ----------------------------------------------------------------------------------
' You could use similar looping techniques with JavaScript
' ----------------------------------------------------------------------------------
%>

<html>
<head></head>
<body>

<form>

<%
' ----------------------------------------------------------------------------------
' Build the MONTH drop-down box
' ----------------------------------------------------------------------------------

iMonth = Month(Now())
' This produces the current month's integer (ex. 7)
%>

<select name="drpMonth">
<% For i = 1 to 12 %>
<option <%
' This will select the current month in the list
If CInt(iMonth = i) Then
Response.Write "selected "
End if
%>value="<%= i %>"><%= i %></option>
<% Next %>
</select>

<!-- Drop-down separator -->
/
<!-- End separator -->

<%
' ----------------------------------------------------------------------------------
' Build the YEAR drop-down box
' ----------------------------------------------------------------------------------

iYear = Year(Now())
' This produces the current year's integer (ex. 2000)
%>

<select name="drpYear">
<% For i = iYear to ( CInt(iYear + 5) ) %>
<option <%
' This will select the current year in the list
If CInt(iYear = i) Then
Response.Write "selected "
End if
%>value="<%= i %>"><%= i %></option>
<% Next %>
</select>

</form>

</body>
</html>

Of course, if you have more complex date interactions, you can manipulate the If/Then statement inside of the loop to select the appropriate value. And, if you're working with dates in the past, you can reindex the starting value of (i) by subtracting how many years you want to go back...

Example:
"For i = iYear to ..." tells the application to do something "From 2000 to ..."
"For i = CInt(iYear - 2) to ..." tells the application to do something "From 1998 to ... "

-END-

时间: 2024-11-10 00:12:27

Dynamic Date Drop-down Boxes的相关文章

jQuery的插件列表(2010-1-25更新)_jquery

jQuery Scroll Follow   随屏幕滚动的层       点击查看Demo jScrollPane 自定义滚动条插件   点击查看Demo  jQuery树状菜单插件 jqueryflexselect下拉框自动提示  jQuery id标签 - 8种不同的很酷的JQuery菜单例子  jQuery ContextMenu:轻量级JQuery插件,可以自定义右键弹出的菜单 jQuery Flash Plugin-这个 jQuery插件可以让你嵌入Flas***** jQuery M

超强1000个jquery插件!

1.导航类 (1)Horizontal accordion: jQuery 基于jQuery开发,非常简单的水平方向折叠控件. 二手图书 二手书 特价书 旧书网 (2)jQuery-Horizontal Accordion 具有XBOX360 blade界面风格的水平方向Accordion. 二手图书 二手书 特价书 旧书网 (3)jQuery plugin: Accordion 用于创建 折叠菜单的jQuery插件.二手图书 二手书 特价书 旧书网 (4)Accordion Menu scri

JQuery知识:20个jQuery教程+11个jQuery插件

在网络发展领域,由于 jQuery 简单易学,易于使用和易于扩展的特点,因此正慢慢变得无处不在.以下是从一些 jQuery 相关文章中整理出来的 30+ 新鲜与惊奇的 jQuery 插件与教程.如果你正在寻找最新的 jQuery 信息,这些内容值得一读. 教程 1. How to add preloader with loading image in a gallery using jQuery在图像画廊载入过程中使用 jQuery 预加载技术.演示 教程 2. Simple Lava Lamp

27个新鲜惊奇的jQuery插件与教程

51CTO推荐专题:jQuery给力插件大阅兵 教程 1. How to add preloader with loading image in a gallery using jQuery 在图像画廊载入过程中使用 jQuery 预加载技术.演示 教程 2. Simple Lava Lamp Menu Tutorial With jQuery 创建一个独特的熔岩灯动画效果的 jQuery 导航菜单.演示 教程 3. How To Create a 3D Tag Cloud in jQuery

dtruss &amp;amp; strace

dtruss for Mac strace for Linux Top 10 DTrace scripts for Mac OS X NAME dtruss - process syscall details. Uses DTrace. SYNOPSIS dtruss [-acdeflhoLs] [-t syscall] { -p PID | -n name | command } DESCRIPTION dtruss prints details on process system calls

日历代码

日历 <html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><meta name="GENERATOR" content="Microsoft FrontPage 4.0"><meta name="ProgId" content="Fron

数据库安全审计

用以下的方式可以监控登入登出的用户: 创建如下的两张表: create table login_log -- 登入登出信息表 ( session_id int not null, -- sessionid login_on_time date, -- 登入进间 login_off_time date, -- 登出时间 user_in_db varchar2(30), -- 登入的db user machine varchar2(20), -- 机器名 ip_address varchar2(20

在RedHat 和 Ubuntu 中配置 Delphi 的Linux开发环境(转)

原文地址:http://chapmanworld.com/2016/12/29/configure-delphi-and-redhat-or-ubuntu-for-linux-development/   方便大家使用,直接转过来.     Image courtesy of Jim McKeeth. One of the more exciting features of the pending 10.2 release of Delphi and RAD Studio, is support

jquery插件整理篇(一)导航类jq插件,菜单jq插件

(1)Horizontal accordion: jQuery 基于jQuery开发,非常简单的水平方向折叠控件. (2)jQuery-Horizontal Accordion 具有XBOX360 blade界面风格的水平方向Accordion. (3)jQuery plugin: Accordion 用于创建 折叠菜单的jQuery插件. (4)Accordion Menu script 基于jQuery开发的可折叠菜单.  (5)模仿ext的tab选项卡 TabPanel(选项卡组件) 参数