使用用于MM、DD和YYYY的下拉框禁用JavaScript日期

使用用于MM、DD和YYYY的下拉框禁用JavaScript日期,javascript,validation,date,Javascript,Validation,Date,我在一个移动网站上工作,有关于月份、日期和年份的下拉框。如果他们选择了过去的日期,我需要一些禁止他们继续下一步的东西。我见过这样做的日历控件,但我不想使用日历控件。我花了一天的大部分时间在找东西,但什么也没找到。有没有人有这样的东西,或者知道我遗漏了什么 功能日期检查() { var trans_date=document.form1.selectmonth.options[document.form1.selectedIndex].value+“-”+document.form1.selec

我在一个移动网站上工作,有关于月份、日期和年份的下拉框。如果他们选择了过去的日期,我需要一些禁止他们继续下一步的东西。我见过这样做的日历控件,但我不想使用日历控件。我花了一天的大部分时间在找东西,但什么也没找到。有没有人有这样的东西,或者知道我遗漏了什么


功能日期检查()
{
var trans_date=document.form1.selectmonth.options[document.form1.selectedIndex].value+“-”+document.form1.selectedDay.options[document.form1.selectedIndex].value+“-”+document.form1.selectedYear.selectedIndex].value;
var d=新日期();
var today=(d.getMonth()+1)+“-”+d.getDate()+“-”+d.getFullYear();
if(新日期(交易日期)<新日期(今天)){
警报(“发货日期不能为过去,请输入有效的发货日期。”);
返回false;
}
}

这是我想出来的,但它不起作用。我还缺什么吗?我将它保留为2011年1月1日,它不会抛出警报。

听起来您只需要一个验证函数,它可以根据所选输入创建一个新日期,并将其与当前日期进行比较。比如:

function isFutureDate(year, month, day) {
    return (Date.parse("" + year + "-" + month + "-" + day)) - new Date() > 0;
}

除非您可能需要考虑时区的变化。

只需从相关元素中获取所选的值,将值与“-”或“/”连接,然后使用
日期
构造函数创建日期对象-将其与当前日期进行比较-如果小于当前日期,则失败

// Inside your form's validation handler ...

var year, month, day, provided_date, now = new Date();

// Remove the hours, minutes and seconds from the now timestamp
now = new Date(now.getYear(), now.getMonth(), now.getDate());

// Get your year select - document.getElementById
// or any other method you have available

year = your_year_select.options[your_year_select.selectedIndex].value;
// and so on for month and day


// Remember, month is 0 indexed in JavaScript (January is month 0)
// so make sure your dropdown values take account of this.
// Otherwise, use month - 1 here.
provided_date = new Date(year, month, day);

// if all you need to do is validate the date
return provided_date >= now;
// Inside your form's validation handler ...

var year, month, day, provided_date, now = new Date();

// Remove the hours, minutes and seconds from the now timestamp
now = new Date(now.getYear(), now.getMonth(), now.getDate());

// Get your year select - document.getElementById
// or any other method you have available

year = your_year_select.options[your_year_select.selectedIndex].value;
// and so on for month and day


// Remember, month is 0 indexed in JavaScript (January is month 0)
// so make sure your dropdown values take account of this.
// Otherwise, use month - 1 here.
provided_date = new Date(year, month, day);

// if all you need to do is validate the date
return provided_date >= now;