我的表单中有一个文本框
<div class="editor-label">
<label>Job Title</label>
</div>
<div class="editor-field">
<input data-val="true" data-val-required="JobTitle is required and cannot be blank" id="JobTitle" name="JobTitle" type="text" value="TBD" />
<span class="field-validation-valid" data-valmsg-for="JobTitle" data-valmsg-replace="true"></span>
</div>
我添加了一个自定义验证器
<script>
$.validator.addMethod('validjobtitle', function (value, element) {
// Test 'value' for html here. 'value' is the value of the control being validated.
if ($('#JobTitle').val() == "TBD") {
alert("test");
return false;
}
else return true; // Return true or false depending on if it passes or fails validation, respectively.
}, 'Fix job Title');
$.validator.unobtrusive.adapters.addBool('validjobtitle');
</script>
在其中一次单击按钮时,我验证了这样的元素
var validator = $("form").validate(); // obtain validator
var anyError = false;
$step.find("input,select").each(function () {
if (!validator.element(this)) {
anyError = true;
}
});
由于某种原因,我的自定义验证器无法正常工作。我究竟做错了什么?