验证 jQuery.validate 和 datepicker 中多个日期之间的日期

发布于 2024-09-24 09:39:46 字数 382 浏览 1 评论 0原文

我正在使用 jQuery.datepicker 和 jQuery.validate。 在我的表单中,我有多个日期范围:

2010-02-10 - 2010-02-18
2010-03-01 - 2010-03-12
2010-03-15 - 2010-03-19

等等。

现在我需要验证日期选择器字段以检查日期选择器中设置的日期是否在这些日期范围之间。例如,2010-01-01 和 2010-02-19 都是无效的。

我看过很多答案,但据我所知,它们与多个范围并没有真正相关。

有谁知道解决方案或至少有一个指针可以让我朝着正确的方向前进。 我正在考虑可能循环 .each() 中的每个日期范围并在那里运行验证。但可能有更好的方法。

I'm using jQuery.datepicker and jQuery.validate.
In my form I have multiple date ranges:

2010-02-10 - 2010-02-18
2010-03-01 - 2010-03-12
2010-03-15 - 2010-03-19

etc.

Now I need to validate the datepicker field to check if the date set in the datepicker is between any of those date ranges. For instance, 2010-01-01 would be invalid, as would 2010-02-19.

I've seen many answers but they don't really relate to multiple ranges, as far as I can see.

Does anyone know a solution or at least a pointer to get me in the right direction.
I'm thinking of maybe looping each date range in a .each() and running a validation in there. But there is probably a better way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

强辩 2024-10-01 09:39:46

你的问题也是我一段时间以来一直想弄清楚的。这是我到目前为止所拥有的。

我用它来验证单个日期范围。它将输入字段作为参数而不是日期:

jQuery.validator.addMethod("rangeDate", function(value, element, params) {
    try {
        var beforedate=$.datepicker.parseDate($(params[0]).datepicker('option','dateFormat'),$(params[0]).val());
        var afterdate=$.datepicker.parseDate($(params[1]).datepicker('option','dateFormat'),$(params[1]).val());
        var qdate=$.datepicker.parseDate($(element).datepicker('option','dateFormat'),value);
        return this.optional(element) || (qdate >= beforedate && qdate<=afterdate);
    } catch(err){
        return false;
    }
}, function(params){
    return "Date must occur between " + $(params[0]).val() + ' and ' + $(params[1]).val();
}
);

并且规则如下所示:

rules:{
  between_date: { dateCan: true, 
         rangeDate:  {
            depends: function(element) { //check that params exist
                return $("input[name='before_date']").valid()
                       && $("input[name='after_date']").valid();
            },
            param:  ["input[name='before_date']", "input[name='after_date']"]
         }
  }
}

经过一番实验后,我发现一个字段可以有多个 rangeDate 规则。但是,最后一次调用似乎会覆盖该字段之前所有 rangeDate 调用的结果。因此,我认为可能有效的是添加另一个自定义规则,该规则接受可以输入到 rangeDate 的字段对数组。这种方法的问题在于,当您获得更多日期对时,rangeDate 规则的 depends 子句变得难以使用。如果您只需要至少一对而不是所有对存在,则尤其如此。

当然,如果日期范围不是动态的,问题会更容易,因为不需要 depends 子句。只需重写 rangeDate 来接受日期而不是字段,并编写一个包装规则来接受日期对数组。

如果您找到了问题的解决方案,我很乐意看到。希望它会比我的更优雅。

Your question is something I've been trying to figure out for a while too. This is what I have so far.

I use this for validating a single date range. It takes input fields as params rather than dates:

jQuery.validator.addMethod("rangeDate", function(value, element, params) {
    try {
        var beforedate=$.datepicker.parseDate($(params[0]).datepicker('option','dateFormat'),$(params[0]).val());
        var afterdate=$.datepicker.parseDate($(params[1]).datepicker('option','dateFormat'),$(params[1]).val());
        var qdate=$.datepicker.parseDate($(element).datepicker('option','dateFormat'),value);
        return this.optional(element) || (qdate >= beforedate && qdate<=afterdate);
    } catch(err){
        return false;
    }
}, function(params){
    return "Date must occur between " + $(params[0]).val() + ' and ' + $(params[1]).val();
}
);

and the rule looks like this:

rules:{
  between_date: { dateCan: true, 
         rangeDate:  {
            depends: function(element) { //check that params exist
                return $("input[name='before_date']").valid()
                       && $("input[name='after_date']").valid();
            },
            param:  ["input[name='before_date']", "input[name='after_date']"]
         }
  }
}

After experimenting a bit, I've seen that it's possible to have more than one rangeDate rule for a field. However, the last call appears to overwrite the results of all the previous rangeDate calls for that field. So I think what might work is to add another custom rule that accepts an array of pairs of fields which can be fed to rangeDate. The problem with this approach is that the the depends clause for the rangeDate rule becomes unwieldy as you get more date pairs. This is especially so if you only require at least one pair to be present rather than all pairs.

Of course, if the date ranges aren't dynamic, the problem is easier because no depends clause is necessary. Just rewrite rangeDate to accept dates rather than fields and write a wrapper rule to accept an array of pairs of dates.

If you've found a solution to the problem, I'd love to see it. Hopefully it will be more elegant than mine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文