0

I am using ko binding for my date time picker. I got a sample from here

But when user enters invalid data the value is resetting to today's date. How to avoid this? (I need to show the field as invalid. Since it is resetting it always shows me valid)

Community
  • 1
  • 1
san
  • 1,859
  • 4
  • 26
  • 38

1 Answers1

1

This isn't due to knockout. This is the default behaviour of the jQuery DatePicker.

When an invalid date is entered, the result of $(element).datepicker('getDate') is today's date.

See here for solutions to check if the current input of a jQuery DatePicker is valid. Then modify the ko.utils.registerEventHandler call in your custom-binding like so:

ko.utils.registerEventHandler(element, "change", function () {

    var observable = valueAccessor();

    var valid;

    /* check validity here */

    if( valid ) {
        observable($(element).datepicker("getDate"));
    }
    else {
        /*
           do something other than jQuery's 'getDate',
           as it will return today's date
        */
    }
});
Community
  • 1
  • 1
Sethi
  • 1,378
  • 8
  • 14