Knockout - validation showing same error message t

2019-08-29 12:47发布

问题:

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" />