Check if dropdown month is eq to current month

For a reservation form I need to check if the chosen day is not the current day and the chosen month is not the current month, since the reservation can not be made on the current day!

The drop down for the months is made with an array of month names:


var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec'];

        for(var i = 0; i < 12; i++){
          var opt = document.createElement('option');
          opt.value = months[i];
          opt.text    = months[i];
          monthdropdown.appendChild(opt);
        }

I tried to check the day and month the following way:


var todaysDate      =  new Date();
var dd                    =  todaysDate.getDate();
var mm                  =  todaysDate.getMonth() + 1;

if(daydropdown <= dd && monthdropdown <= mm )
{
            $("#send_status").html('<div class="error" style="color: #C00 !important;">Reservations for today can only made by telephone!!.</div>');
            $("#daydropdown").focus();
}

But the month check isn’t working since the month dropdown is build up from the array. So how do I check if the chosen month is the current month.

Thank you in advance.

Hi donboe,

It all depends on what daydropdown and monthdropdown are.
They sound like select elements and it is their value (not the elements themselves) that you would need to check

As ever, could you post a link of some HTML?

Hi Pullo. They are indeed select boxes, and this is the link to the particular page. Thank you in advance :tup:

Hi donboe,

Just looking at this now.
You have three (plus) elements with the id “form_wrapper”.
You will need to sort this.
I’ll get back soon with the rest.

Hi donboe,

What about:

$("#daydropdown, #monthdropdown, #yeardropdown").on("change", function(){
  var today = new Date(),
      dd = today.getDate(),
      mm = today.getMonth() + 1;
      selDay = Number($("#daydropdown").val()),
      selMonth = Number($("#monthdropdown").val());

  if (dd === selDay && mm === selMonth){
    alert("You can't reserve for today's date!")
  }
})

As an aside, this seems quite counter intuitive to me.
Would it not be better to remove dates that it’s not possible to select?

Hi Pullo.

This would be a good solution indeed

How would I accomplish that the past days (incl today) are not showing in the dropdown menu?

Thank you in advance.

I would recommend using a server-side language.
You could do this in JS, but if the user doesn’t have their system clock set correctly, it could produce unexpected results.

Do you have access to PHP?

Yes I do!

Well, as server time is more reliable than client time, I would make a small PHP script to return the current year, month and day.
I would then fire off an AJAX request to this script on page load to fetch these variables.
Once you have them, you can just adapt your JS routine to not show anything before today’s date.