我需要使用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()
答案 0 :(得分:3)
问题是因为所需规则的data
属性不正确。您需要设置data-rule-required="true"
,然后将错误消息放入data-msg-required
。试试这个:
$('#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;