简体   繁体   English

跟踪淘汰赛中输入值的变化

[英]Track change in value of a input in knockout js

In my Knockout.js application I want to validate the user input. 在我的Knockout.js应用程序中,我想验证用户输入。 I have used custom validation 我使用过自定义验证

Below is the code i have used looping through each element in the array. 下面是我使用循环遍历数组中每个元素的代码。

result.Settings.filter(function (element) {
    element.DisplayMobile = ko.observable(element.PointsForMobile).extend({ required: { message: 'This field cannot be empty' }, useNumberFloatOnly: "abc", maximumValue: 'abc'});
    element.DisplayWeb = ko.observable(element.PointsForWeb).extend({ required: { message: 'This field cannot be empty' }, useNumberFloatOnly: "abc", maximumValue: 'abc'});

    element.Error = ko.observable(false);
});

below is the input in the view 下面是视图中的输入

<input type="number" data-bind="value:$data.DisplayWeb,valueUpdate: ['afterkeydown', 'input'],,change:TestValidPoint" class="positiveno" min="0" />
<input type="number" data-bind="value:$data.DisplayMobile,valueUpdate: ['afterkeydown', 'input']" class="positiveno" min="0" />

below is the useNumberFloatOnly validator which works for me. 下面是useNumberFloatOnly验证器,它适用于我。

ko.validation.rules['useNumberFloatOnly'] = {
            validator: function (val, othervalue) {
                var numStr = /^\d*[0-9]\d*$/;

                if (othervalue === "abc") {
                    Settings().filter(function (element) {
                        if (element.DisplayMobile() == "" || element.DisplayWeb() == "") {
                            element.Error(true);

                        }
                        if ((element.DisplayMobile() == val || element.DisplayWeb() == val ) && !numStr.test(val)) {
                            element.Error(true);

                        }
                        else if ((element.DisplayMobile() == val || element.DisplayWeb() == val) && numStr.test(val)) {
                            element.Error(false);
                        }
                    });
                }
                return numStr.test(val);
            },
            message: 'Enter only positive values.Decimals not allowed'
        };

on click of the save button i want to check if any of the fields has any error. 单击保存按钮我想检查是否有任何字段有任何错误。 two problems i'm facing 我面临的两个问题

1) How to track changes in the input value? 1)如何跟踪输入值的变化? what event to use for that? 用什么事件? this is for tracking if the input has error and if i can disable the save button . 这是为了跟踪输入是否有错误,以及我是否可以禁用保存按钮。

2) Second thing i tried is on the save button click event on looping through the array the Error observable is always false, even though i'm setting the value as true in the validator. 2)我尝试的第二件事是在循环遍历数组的保存按钮单击事件上, Error observable始终为false,即使我在验证器中将值设置为true。

Please guide 请指导

Thanks 谢谢

You can create a errorGroup for all the observable s for which you want the validation. 您可以为要进行验证的所有observable创建errorGroup Then use errorGroup.isValid() to conditionally enable the button 然后使用errorGroup.isValid()有条件地enable该按钮

 const viewModel = function() { this.DisplayMobile = ko.observable().extend({ required: true }); this.DisplayWeb = ko.observable().extend({ required: true }); this.submit = function() { if (this.errorGroup.isValid()) { alert('submitted'); } else { this.errorGroup.errors.showAllMessages(); } }; this.errorGroup = ko.validatedObservable({ DisplayMobile: this.DisplayMobile, DisplayWeb: this.DisplayWeb }); }; ko.applyBindings(new viewModel()); 
 .validationMessage{ color:red } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout-validation/2.0.3/knockout.validation.min.js"></script> <fieldset> <legend>Details</legend> <label>Display Mobile: <input data-bind='value: DisplayMobile' /> </label><br> <label>Display Web: <input data-bind='value: DisplayWeb' /> </label> </fieldset> <br> <button type="button" data-bind='click: submit, enable: errorGroup.isValid()'>Submit</button> <br> <br> Error count: <span data-bind='text: errorGroup.errors().length'></span> 

Another similar approach is to use a validation group: 另一种类似的方法是使用验证组:

 this.errors = ko.validation.group(this):

 const viewModel = function() { this.DisplayMobile = ko.observable().extend({ required: true }); this.DisplayWeb = ko.observable().extend({ required: true }); this.submit = function() { if (this.errors().length === 0) { alert('submitted'); } else { this.errors.showAllMessages(); } }; this.errors = ko.validation.group(this); }; ko.applyBindings(new viewModel()); 
 .validationMessage{ color:red } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout-validation/2.0.3/knockout.validation.min.js"></script> <fieldset> <legend>Details</legend> <label>Display Mobile: <input data-bind='value: DisplayMobile' /> </label><br> <label>Display Web: <input data-bind='value: DisplayWeb' /> </label> </fieldset> <br> <button type="button" data-bind='click: submit, enable: errors().length === 0'>Submit</button> <br> <br> Error count: <span data-bind='text: errors().length'></span> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM