使用jquery.validate.unobtrusive验证复选框

时间:2017-07-19 11:33:53

标签: javascript jquery validation checkbox

我需要使用jquery.validate.unobtrusive验证lows复选框,我使用此代码,但始终复选框有效。怎么解决这个?

<input id="AcceptLaws" type="checkbox" name="AcceptLaws" data-val="true" data-val-required="Please accept with laws">
<span class="field-validation-valid" data-valmsg-for="AcceptLaws" data-valmsg-replace="true"></span>
$('#AcceptLaws').valid()

1 个答案:

答案 0 :(得分:3)

问题是因为所需规则的data属性不正确。您需要设置data-rule-required="true",然后将错误消息放入data-msg-required。试试这个:

&#13;
&#13;
$('#AcceptLaws').valid()
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.16.0/jquery.validate.min.js"></script>

<form>
  <input id="AcceptLaws" type="checkbox" name="AcceptLaws" data-val="true" data-rule-required="true" data-msg-required="Please accept with laws" />
  <span class="field-validation-valid" data-valmsg-for="AcceptLaws" data-valmsg-replace="true"></span>
</form>
&#13;
&#13;
&#13;