• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

Dynamic Date Drop-down Boxes

发布: 2007-6-30 18:56 | 作者: admin | 来源: | 查看: 14次 | 进入软件测试论坛讨论

领测软件测试网 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-

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网