【发布时间】:2012-08-31 15:16:08
【问题描述】:
我正在尝试验证日期输入字段。 我可以阻止 jquery datepicker 使用,以防止输入旧日期,
$('#delivery_date').datePicker({ minDate: 0 });
但用户仍然可以手动输入日期。如何防止或验证这一点?
【问题讨论】:
标签: jquery validation date
我正在尝试验证日期输入字段。 我可以阻止 jquery datepicker 使用,以防止输入旧日期,
$('#delivery_date').datePicker({ minDate: 0 });
但用户仍然可以手动输入日期。如何防止或验证这一点?
【问题讨论】:
标签: jquery validation date
// 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());
}
})
【讨论】:
$('#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());
});
【讨论】: