简体   繁体   English

验证包含日期和时间字段的表单,这些表单一起必须描述将来的日期和时间

[英]Validating a form containing a date and a time field that together must describe a date and time in the future

I have a form containing a lot of fields that all have some sort of validation. 我有一个包含很多字段的表单,这些字段都有某种形式的验证。 Two fields however are special: a date field and a time field. 但是,有两个特殊的字段:日期字段和时间字段。 Their validation states that their combined date/time value is a timestamp somewhere in the future. 他们的验证表明,它们组合的日期/时间值是将来某个地方的时间戳。 I use the jQuery validation plugin plus its extra functions to do the validation. 我使用jQuery验证插件及其额外功能来进行验证。 Check out this fiddle to see a trimmed down version of what I have: 看看这个小提琴,看看我拥有的精简版:

http://jsfiddle.net/2df9h/4/ http://jsfiddle.net/2df9h/4/

I have a couple of problems: 我有几个问题:

  1. When I fill in todays date in the date field and a time in the past in the time field the validation fails as expected (the timestamp is in the past). 当我在日期字段中填写今天的日期,在时间字段中填写过去的时间时,验证将按预期失败(时间戳是过去的日期)。 However: only the time field gets the error view which is correct plugin behavior, but I'd like both fields to show the error. 但是:只有时间字段会得到错误视图,这是正确的插件行为,但是我希望两个字段都显示错误。 How can I do this? 我怎样才能做到这一点?
  2. When both fields are showing the 'future' error and I fix it by changing one field the other one does not switch to fixed. 当两个字段都显示“未来”错误时,我通过更改一个字段来解决该错误,而另一个字段未切换为已修复。 This too is expected behavior, but I'd like both fields to be cleared of the error immediately . 这也是预期的行为,但我希望立即清除两个字段中的错误。 How can I do this? 我怎样才能做到这一点?

I have tried to validate the other field using the technique described here : $(.. selector for the two fields...).valid() , but due to a bug described in this comment in that post the other fields will no be validated anymore when I do that. 我试图使用此处描述的技术来验证另一个字段: $(.. selector for the two fields...).valid() ,但是由于该评论中描述的错误,其他字段将不会当我这样做时,已经验证了。 I'm pretty much stuck... Any idea? 我几乎被困住了...知道吗?

You can force (or clear) an error on any field programatically with the method .showErrors(errors) (see documentation ). 您可以使用.showErrors(errors)方法.showErrors(errors)编程方式在任何字段上强制(或清除) .showErrors(errors) (请参阅文档 )。

This should help overcome both issues. 这应该有助于克服这两个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM