0

我正在尝试验证日期输入字段。我可以阻止 jquery datepicker 以防止使用输入旧日期,

 $('#delivery_date').datePicker({ minDate: 0 }); 

但用户仍然可以手动输入日期。如何防止或验证这一点?

4

2 回答 2

1
// when the field changes value
$('#delivery_date').on('change',function(){
    var today = new Date();
    today = new Date(today.getYear(), today.getMonth(), today.getDate());
    // compare the value of the field with whatever you want to test against
    if($(this).val() > today){
         // if the test fails, change the value to default
         $(this).datepicker('setDate', new Date());
    }
})
于 2012-08-31T15:21:50.933 回答
0
$('#delivery_date').on('change', function(){ 
    var today = new Date();
    if(new Date($(this).val()) < new Date(today.getYear(), today.getMonth() - 1, today.getDate()) $(this).datepicker('setDate', new Date());
});
于 2012-08-31T15:18:47.717 回答