I have a very large Form with many date fields that need to be validated. For the most part, this is pretty straight-forward to do with the jQuery Validation plug-in. But I have found a use-case, where I am not exactly sure that the "correct" design decision is:
I have a date-field whose value is by default an abstract example :
<input id="date_field" name="date_1" class="" type="text" value="tt.mm.jjjj" maxlength="10"/>
I want this field to be optional. So in the validator option I declare:
date_field: {date: true}
Ofcourse this isn't correct, because if the user hasn't entered anything and thus left the default "dd.mm.yyyy" value, the validator will check to see if the value is a date, which it's not and throw an error.
I could write my own custom date validator that just adds the "dd.mm.jjjj" to the list of correct dates. But in a different use case in the same form, I have a field that is both a date and required. If I used the custom validator on this field, then the开发者_运维知识库 form would validate even if the user hasn't entered anything on his own. But because the custom validator allows for the "dd.mm.jjjj" String to be accepted , and that string field is obviously not empty, the form would be accepted.
So, the way I see it the optimal solution would be to "extend" the cases that the validator interprets a field as beeing empty.
In other words I would like to say to the validator: "Hey, date fields are also empty if they have this string as a value".
Unfortunatelly my jQuery/ Javascript skills are somewhat limited and my attemts have so far failed to construct anything like the above.
On the other hand, this must be a pretty common problem but I haven't found a documented solution to it. So maybe I am missing something?
Thank you!
I ended up just extending jQuery Validator with my own custom "required" rule, just like the localization of "date" does. Was pretty obvious:)
精彩评论