简体   繁体   中英

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. Check out this fiddle to see a trimmed down version of what I have:

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. 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 ).

This should help overcome both issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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