Skip to main content Skip to footer

Prevent Users from Selecting Dates Outside of the Current Month in JavaScript

Background:

By default, the Calendar control will show all dates of the current month, as well as some dates in the previous/next month in order to fill up all 30 date slots in the display. This means that users can select dates from the previous/next month without changing the current month. In some scenarios, developers want to prevent users from being able to select those dates.

Steps to Complete:

1. Set the min and max values of the calendar to be the first and last days of the current month

2. Add the calendar buttons to an array to allow us to tie events to them

3. Use the events tied to the calendar buttons to change the min and max values when the month changes

Getting Started:

Set the min and max values of the calendar to be the first and last days of the current month

In order to set the min and max values, you’ll simply need to pass in a date value to those properties of the calendar control.

var theCalendar = new wijmo.input.Calendar('#theCalendar', {
  valueChanged: function(s,e) {
    showCurrentDate();
  },
  min: new Date(2020, 1, 1),
  max: new Date(2020, 1, 29),
})

This sets the minimum selectable date to the first of Feb, 2020 and the maximum date to the last of Feb 2020.

Add the calendar buttons to an array to allow us to tie events to them

In the case of the sample, we’re going to be using the getElementsByTagName to return an array of buttons on the page; in this case, we’ll only need 2: the first button of the page, and the third button of the page. These are the ‘Previous Month/Next Month’ buttons that the user will use to navigate the component.

var monthButtons - document.getElementsByTagName('buttons');
monthButtons[0].onclick = function(){}
monthButtons[2].onclick = function(){}

Use the events tied to the calendar buttons to change the min and max values when the month changes

Now that we have events tied to the buttons for when they get clicked, all we need to do now is modify the min and max value of the component when one of these buttons is clicked, in order to reflect the new month that the user is viewing.

monthButtons[0].onclick = function() {
  var previousMonth = getCurrentMonth() - 1;
  if(previousMonth < 0) {
    currentYear = currentYear - 1;
    previousMonth = 11;
  }
  theCalendar.min = new Date(currentYear, previousMonth, 1);
  theCalendar.max = new Date(currentYear, previousMonth, numberOfDays[previousMonth]);
  theCalendar.displayMonth = theCalendar.min;
}

monthButtons[2].onclick = function() {
  var nextMonth = getCurrentMonth() + 1;
  if(nextMonth > 11) {
    currentYear = currentYear + 1;
    nextMonth = 0;
  }
  theCalendar.min = new Date(currentYear, nextMonth, 1);
  theCalendar.max = new Date(currentYear, nextMonth, numberOfDays[nextMonth]);
  theCalendar.displayMonth = theCalendar.min;
}

You can find a live sample of the project in this article here: http://jsfiddle.net/JParksGC/v00sx2tt/

Joel Parks