验证 jQuery.validate 和 datepicker 中多个日期之间的日期
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的问题也是我一段时间以来一直想弄清楚的。这是我到目前为止所拥有的。
我用它来验证单个日期范围。它将输入字段作为参数而不是日期:
并且规则如下所示:
经过一番实验后,我发现一个字段可以有多个
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:
and the rule looks like this:
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 previousrangeDate
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 torangeDate
. The problem with this approach is that the thedepends
clause for therangeDate
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 rewriterangeDate
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.