首页 > 解决方案 > 如果字段具有默认值,如何使用验证插件对输入字段进行必需

问题描述

我有一个表格,如果在其中选择或填写了任何其他字段,我正在使用验证插件。

我想要的是,如果输入字段有一个默认值,比如给定value="0.00"它不应该使其他输入字段成为必需,只有在手动填写时才成为必需。

<div class="col-md-3">
  <div class="form-group">
    <label>From Recognition Score</label>
    <input type="text" name="from_individual_recognition_score_3rd" class="form-control m2 validate[condRequired[recognize_1,recognize_3,recognize_4],min[1]]" id="recognize_2" maxlength="5" value='0.00'>
  </div>
</div>

感谢和问候

标签: javascriptjqueryjquery-validate

解决方案


仅在手动填写时才需要

这没有任何意义。required如果已经填满了东西,你怎么能创建一个字段?只能制作空字段required。一旦它们包含一个值,它们就不再是“必需的”,因为required验证规则已完全满足。

使用该placeholder属性,因为这就是它的用途;向用户显示默认值或示例值。

Showing the default value using placeholder attribute:

<input type="text" placeholder="0.00" name="from_individual_recognition_score_3rd" />

否则,也许您可​​以编写一个对您的项目更有意义的自定义规则/方法。由于您在 OP 中显示的代码非常少,因此无法知道在这里可以使用什么。


推荐阅读