jQuery(document).ready(function() { jQuery("#btnGetMatches").click(buttonClick); }); function buttonClick(event) { var yearToMatch = jQuery("#txtYearToMatch").val(); var startYear = jQuery("#txtStartYear").val(); var endYear = jQuery("#txtEndYear").val(); // Make sure all three inputs are years >= 1582. if (!isYear(yearToMatch) || !isYear(startYear) || !isYear(endYear)) { alert("Enter year and range, then click button."); return; } if ((endYear - startYear) > 400) { alert("Range must be <= 400"); return; } // First day of year (month 0, day 1). var d = new Date(yearToMatch, 0, 1); // Day of week for first day of year (0 = Sun, 1 = Mon, ..) var firstDay = d.getDay(); // Have leap year iff February has 29 days. isLeapYear = ((new Date(yearToMatch, 1, 29)).getMonth() == 1); // To hold matching years. var matches = new Array(); // Have matching year iff first day and leap year status the same. for (var year = startYear; year <= endYear; year++) { var d1 = new Date(year, 0, 1); var firstDay1 = d1.getDay(); isLeapYear1 = ((new Date(year, 1, 29)).getMonth() == 1); if ((firstDay1 == firstDay) && (isLeapYear1 == isLeapYear)) matches.push(year); } // for year // html to poke into result box at bottom. var html = ""; if (matches.length == 0) { html = "No matches for " + yearToMatch + " between "; html += "" + startYear + " and " + endYear + "." } else { html = "Matches for " + yearToMatch + ": "; html += "

"; for (var k = 0; k < matches.length; k++) html += "" + matches[k] + ", "; // Remove last comma and space. html = html.substring(0, html.length - 2); html += "

"; } jQuery("#matches").html(html); } // function buttonClick function isYear(year) { var regExp = /^\d{4}$/; return (regExp.test(year) && (year >= 1582)); }