我使用敲除验证插件来验证一个简单的表单字段,验证工作正常,但它在文本框下方显示两次相同的错误消息。
我的代码如下
JS viewmodel
$(document).ready(function () {
ko.validation.registerExtenders();
ko.validation.configure({
registerExtenders: true,
messagesOnModified: false,
insertMessages: false,
parseInputAttributes: true,
messageTemplate: null
});
ko.validation.init();
var vm = new viewmodel();
ko.applyBindings(vm, document.getElementById("div"));
});
that.formField= ko.observable(vm.formField).extend({ required: true, minLength: 5, maxLength: 50 });
**html**
<p>
<label class="field-label">Who provides your service?</label>
<input name="txtService" id="txtInsservice" data-bind="value: formField, valueUpdate: 'keyup'" class="field-stretch" type="text" maxlength="50" />
</p>
这有什么不对吗?
答案 0 :(得分:0)
您应该将验证选项放在javascript中的html中。在这里你做两件事。 我建议只将它们放到javascript中,并将其从html中删除,如下所示:
<input name="txtService" id="txtInsservice" data-bind="value: formField, valueUpdate: 'keyup'" class="field-stretch" type="text" />
答案 1 :(得分:0)
问题可能是您在js文件下两次调用了
knockout.validation.js