Javascript日期验证

时间:2011-09-21 07:39:19

标签: javascript jquery calendar

我有两个文字字段

<input type="text" name="fromdate" > (YYYY-MM-DD)

<input type="text" name="todate" > (YYYY-MM-DD)

fromdate和todate都有日期字段。

Dateformat是(YYYY-MM-DD)

在这些需要验证todate textfield的日期中,date应该大于fromdate EX:如果fromdate是2011-11-24 todate应该大于fromdate,todate可能25,26等..,需要在javascript中验证todate textfield

4 个答案:

答案 0 :(得分:2)

如果你使用datePicker插件,你可以这样做:

$('#from').datepicker({
    dateFormat: 'yy-mm-dd',
    onSelect: function(dateText, inst) {
        $('#to').datepicker('option', 'minDate', dateText);
    }
});

$('#to').datepicker({
    dateFormat: 'yy-mm-dd',
    onSelect: function(dateText, inst) {
        $('#from').datepicker('option', 'maxDate', dateText);
    }
});

在这里摆弄http://jsfiddle.net/PAsnc/

答案 1 :(得分:0)

您可以将datepicker插件与jquery一起使用。

使用minDate和maxDate

可以选择将日期限制为任何一天

答案 2 :(得分:0)

我使用datejs:http://www.datejs.com/

尤其是Date.compare

  

将第一个日期与第二个日期进行比较,并返回其相对值的数字指示。 -1 =这比日期少。 0 =值相等。 1 =这比日期更重要。

答案 3 :(得分:0)

开始使用html5可能是一件好事:

<input type="date" name="fromdate" />
<input type="date" name="todate" />

在Opera和Chrome中它可以使用,但对于旧版浏览器,您需要一个后备herehere with modernizr,就像其他用户建议的那样。