【问题标题】:jQuery Validation DOB check works in Chrome but fails in SafarijQuery Validation DOB 检查在 Chrome 中有效,但在 Safari 中失败
【发布时间】:2013-09-26 15:57:21
【问题描述】:

我有一个超级简单的 jQuery 验证方法,它在出生日期字段上运行。它所做的只是检查输入的日期(来自 HTML5 输入 type=date 字段)是否早于当前日期。如果是,则有效,否则无效。它在 Chrome 中运行良好,但由于某种原因在 Safari 中失败,就像输入了无效日期一样。代码如下:

$.validator.addMethod("bornBeforeToday", function(value, element) {
    // For DOB, check that value given is before the current date
    if (value.length > 0) {
        return Date.parse(value) < new Date();
    } else {
        return true;
    }
}, "Date must be less than than current date");

【问题讨论】:

  • 是抛出错误还是根本没有运行?
  • 抛出有关不正确日期的错误,因此它正在运行但未正确验证日期。
  • 尝试将返回标志设置为一个变量,并且只有 1 个返回语句。
  • Date.parse(value) 返回一个类似-1775674800000 的数字,但是,new Date() 返回Thu Sep 26 2013 12:57:01 GMT-0500 (CST)

标签: jquery html date safari jquery-validate


【解决方案1】:

Javascript Date 方法依赖于浏览器。在验证范围之前尝试使用 dateUS 或 dateISO 方法。

【讨论】:

    猜你喜欢
    • 2012-11-12
    • 2018-02-21
    • 1970-01-01
    • 2016-03-28
    • 2017-01-22
    • 2015-12-26
    • 1970-01-01
    • 1970-01-01
    • 2014-01-11
    相关资源
    最近更新 更多