Credit Card Expiration Date DropDownList Sample Code

The other day I needed to create a credit card input form including the drop down lists for the credit card expiration month and year.  I started to write the code to populate the month and year drop down lists and I wanted to make them dynamic so that whatever the year was it would add an appropriate number of list items.  But as I started thinking about how to structure the code, I figured that this has been written probably thousands of times by other developers so I would just Google for it.  To my surprise, I was unable to pull back any sample code for my various search terms.  In the end I had to write it myself but now I am posting it on my blog so that others my benefit from my hard work.  :)

//Populate the credit card expiration month drop down
for (int i = 1; i <= 12; i++)
{
    DateTime month = new DateTime(2000, i, 1);
    ListItem li = new ListItem(month.ToString("MMM (M)"), month.ToString("MM"));
    ExpirationDateMonthDropDown.Items.Add(li);
}
ExpirationDateMonthDropDown.Items[0].Selected = true;

//Populate the credit card expiration year drop down (go out 12 years) 
for (int i = 0; i <= 11; i++)
{
    String year = (DateTime.Today.Year + i).ToString();
    ListItem li = new ListItem(year, year);
    ExpirationDateYearDropDown.Items.Add(li);
}
ExpirationDateYearDropDown.Items[0].Selected = true;

 

Technorati Tags:

Published 08 September 2009 09:02 PM by Jeff Widmer
Filed under: , ,

Comments

# John Sheehan said on 09 September, 2009 12:04 AM

Setting the selected property is unnecessary since the first item in the dropdown is selected by default.

# http://tipsforcdevelopers.blogspot.com/ said on 09 September, 2009 02:08 AM

Thanks for sharing this code with all of us. I have a recommondation for the hardcoded numbers in your code. For improving the readability use constants for 12 (of months), 2000 (in the loop of the month and 11 (years).

# webbes said on 09 September, 2009 06:49 AM

This code is not really well written actually. You create a lot of DateTime variables which you don't need at all.

           CultureInfo ui = CultureInfo.CurrentUICulture;

           string[] monthNames = ui.DateTimeFormat.MonthNames;

           int monthsInYear = ui.Calendar.GetMonthsInYear(DateTime.Now.Year);

           int monthNumber = 0;

           var monthsDataSource = monthNames.Take(monthsInYear).Select(monthName => new {

               Name = monthName,

               Value = ++monthNumber

           });

           monthsDropDownList.DataTextField = "Name";

           monthsDropDownList.DataValueField = "Value";

           monthsDropDownList.DataSource = monthsDataSource;

           monthsDropDownList.DataBind();

Would be better.. imho.

Cheers,

Wes

# Twitter Trackbacks for Credit Card Expiration Date DropDownList Sample Code - Jeff Widmer's Blog [asp.net] on Topsy.com said on 09 September, 2009 01:23 PM

Pingback from  Twitter Trackbacks for                 Credit Card Expiration Date DropDownList Sample Code - Jeff Widmer's Blog         [asp.net]        on Topsy.com

# Credit Card Expiration Date DropDownList Sample Code – Jeff … « Site2Next said on 09 September, 2009 10:06 PM

Pingback from  Credit Card Expiration Date DropDownList Sample Code &#8211; Jeff &#8230; &laquo;  Site2Next

# Aaron said on 17 September, 2009 11:02 AM

Thanks for the code, Jeff! In the past the company that I work for had hard-coded years into credit card expiration date drop down lists which is a big pain to update.

You could also set the selected month to the current month like:

ExpirationDateMonthDropDown.SelectedValue = DateTime.Now.ToString("MM");

That way the default selected month/year value isn't in the past.

# Bill Hughes said on 26 October, 2009 11:08 PM

Thank you for all your hard work - the code worked great and saved me alot of time.  Your the best..

# defiestc said on 27 October, 2009 09:31 PM

Thanks for the sample!  Appreciate it very much...

# schlub said on 22 November, 2009 10:34 PM

I find this works well for my CascadingDropDown and you're not creating any DateTime variables. If you want the month as an integer for the value just remove change the value to "i.Tostring()" instead.

               [WebMethod]

public CascadingDropDownNameValue[]

GetMonths(string knownCategoryValues, string category)

{

List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();

//Get the next 12 months

for (int i = 1; i <= 12; i++)

{

values.Add(new CascadingDropDownNameValue(i.ToString("D2"), i.ToString("D2")));

}

return values.ToArray();

}

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Search

Go

This Blog

News

Recommended Blogs

Syndication