Knockout - validation showing same error message twice

倾然丶 夕夏残阳落幕 提交于 2019-12-25 02:45:28

问题


I am using knockout validation plugin to validate a simple form field , validation is working but it is showing same error message twice below text box.

my code follows
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>

Anything wrong with this?


回答1:


You should put the validation options in the html OR in the javascript. Here you are doing both. I suggest putting them only to the javascript and remove them from the html like this:

<input name="txtService" id="txtInsservice"  data-bind="value: formField, valueUpdate: 'keyup'" class="field-stretch" type="text" /> 


来源:https://stackoverflow.com/questions/22399928/knockout-validation-showing-same-error-message-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!