JavaScript select date: Days select box options set dynamically depends on Months and Years

There is one more interesting issue.
ASPX page has 3 select boxes to enter user’s date of birth:
Months, Days, Years

The problem is that we need dynamically correct options of Days select box.
It depends on values currently selected in other select boxes Months and Years.

jQuery(function() {
var dMonthID = ‘<%=dMonth.ClientID%>’;
var dDayID = ‘<%=dDay.ClientID%>’;
var dYearID = ‘<%=dYear.ClientID%>’;
var cal_days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

var safeToInt = function(n) {
try {
n = parseInt(n);
} catch (e) { n = 0; }
return n;
}
var updateDays = function() {
var nDays = 31;
var nMonth = safeToInt(jQuery(‘#’ + dMonthID).val());
if (nMonth > 0)
nDays = cal_days_in_month[nMonth – 1];
if (nMonth == 2) {
var nYear = safeToInt(jQuery(‘#’ + dYearID).val());
if ((nYear % 4 == 0 && nYear % 100 != 0) || nYear % 400 == 0)
nDays = 29;
}
var days = jQuery(‘#’ + dDayID);
var current = days.val();
var sDays = ‘<option value=”-1″>-Day-</option>’;
for (var i = 1; i <= nDays; ++i)
sDays += ‘<option value=”‘ + i + ‘”>’ + i + ‘</option>’;
days.html(sDays);
days.val(current);
}
jQuery(‘#’ + dMonthID).change(function() { updateDays(); });
jQuery(‘#’ + dYearID).change(function() { updateDays(); });
updateDays();
});
//–></script>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: