【问题标题】:Date validation using jquery, prevent entering yeterdays date.使用 jquery 进行日期验证,防止输入今天的日期。
【发布时间】:2012-08-31 15:16:08
【问题描述】:

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

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

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

【问题讨论】:

    标签: jquery validation date


    【解决方案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());
        }
    })
    

    【讨论】:

    • 谢谢大家......我会试试这些。作为一项临时措施,我将日期输入字段设为只读......输入日期的唯一方法是使用 datepicker 进行选择。在日期选择器中,我禁用了过去的日期。
    • 请记住,所有客户端代码都可以被绕过和覆盖。您必须复制验证服务器端以确保数据完整性 - 客户端验证只是为了方便用户。
    【解决方案2】:
    $('#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());
    });
    

    【讨论】:

    • 您好,您能否确认 maxDate 正在工作,因为它对我不起作用,($('#delivery_date').datePicker({startDate: '01/01/1970', maxDate: " +5"});) 对日期选择没有任何影响,因为我可以选择任何日期。
    猜你喜欢
    • 2014-04-23
    • 2014-08-14
    • 1970-01-01
    • 2019-02-28
    • 2011-01-16
    • 1970-01-01
    • 1970-01-01
    • 2016-02-20
    • 1970-01-01
    相关资源
    最近更新 更多