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.
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
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.
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.