在 jQuery 验证中将特定字段值定义为语义上等于空

发布于 2024-09-20 00:33:00 字数 821 浏览 1 评论 0原文

我有一个非常大的表单,其中有许多需要验证的日期字段。在大多数情况下,使用 jQuery 验证插件可以非常简单地完成此操作。但我发现了一个用例,我不确定“正确”的设计决策是:

我有一个日期字段,其默认值是一个抽象示例:

<input id="date_field" name="date_1" class="" type="text" value="tt.mm.jjjj" maxlength="10"/>

我希望该字段是可选的。因此,在验证器选项中我声明:

date_field:         {date: true}

当然这是不正确的,因为如果用户没有输入任何内容并因此保留默认的“dd.mm.yyyy”值,验证器将检查该值是否是日期,但事实并非如此,并抛出错误。

我可以编写自己的自定义日期验证器,只需将“dd.mm.jjjj”添加到正确日期列表中。 但在同一表单的不同用例中,我有一个既是日期又是必填的字段。如果我在此字段上使用自定义验证器,那么即使用户没有自己输入任何内容,表单也会进行验证。但是因为自定义验证器允许接受“dd.mm.jjjj”字符串,并且该字符串字段显然不为空,因此该表单将被接受。

因此,我认为最佳解决方案是“扩展”验证器将字段解释为空的情况。

换句话说,我想对验证器说:“嘿,如果日期字段将此字符串作为值,那么它们也是空的”。

不幸的是,我的 jQuery/Javascript 技能有些有限,到目前为止我的尝试未能构建类似上述的东西。

另一方面,这肯定是一个非常常见的问题,但我还没有找到解决方案。所以也许我错过了什么?

谢谢你!

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!

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

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

发布评论

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

评论(1

铁憨憨 2024-09-27 00:33:00

我最终只是用我自己的自定义“必需”规则扩展了 jQuery Validator,就像“日期”的本地化一样。非常明显:)

I ended up just extending jQuery Validator with my own custom "required" rule, just like the localization of "date" does. Was pretty obvious:)

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