san
san

Reputation: 1859

knockout validation on delay

I am new to knockout js. I need to have a validator for date which user will be typing in textbox. for this wrote the code like

ko.validation.rules['date'] = {
    validator: function (value, validate) {
      //Custom logic
    },
    message: 'Please type proper date'
};

self.userDate = ko.observable(new Date()).extend({date: true });

This is working fine on tab out. But i need to call this validation on some delay(When the user stopped typing).

Any one could tell me how can i call this validation on delay?

Upvotes: 2

Views: 2879

Answers (1)

Anders Arpi
Anders Arpi

Reputation: 8397

To make sure the viewmodel is updated as the user is typing, use the valueUpdate binding:

<input data-bind="value: userDate, valueUpdate: 'afterkeydown'" />

You then throttle the observable:

self.userDate = ko.observable(new Date()).extend({
    throttle: 1000, //<- time in ms to wait before validation
    date: true
});

Throttle in this case waits 1000 ms after the last registered input event to perform the validation.

Upvotes: 8

Related Questions