Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knockout date validation not working correctly

I need to validate date in localized format (SK) and it is not possible with knockout validation.

I am using: Durandal 1.2.0 Knockout 2.3.0 Knockout validation https://github.com/Knockout-Contrib/Knockout-Validation

I need something like this to be working:

var newStartDate = ko.observable()
.extend({ 
required: { message: 'Start date is required.' }, 
date: true, 
formattedDate: { format: 'ddMMyyyy', delimiter: '.' } 
});

Then if I call newStartDate.isValid() I get these results:

newStartDate("");
false (OK)

newStartDate("test");
false (OK)

newStartDate("2013-02-02");
true (Bad)

newStartDate("2.2.2013");
false (Bad)

The results should be:

newStartDate("2013-02-02");
false

newStartDate("2.2.2013)"; // or "2. 2. 2013"
true

I have some solution with custom validation inside of extend function, but that is not what I want. I want to have block above working, it's also shorter.

like image 472
user2711254 Avatar asked Dec 29 '25 17:12

user2711254


2 Answers

A little late by why not... The date rule does seem a bit too permissive for my uses as well. If you like moment but want something cleaner for the caller, add your rule to the validation.rules object:

ko.validation.rules['simpleDate'] = {
    validator: function (val, validate) {
        return ko.validation.utils.isEmptyVal(val) || moment(val, 'MM/DD/YYYY').isValid();
    },
    message: 'Invalid date'
};

And then use it like:

var someDate= ko.observable().extend({ simpleDate: true });
like image 147
ChoptimusPrime Avatar answered Jan 01 '26 06:01

ChoptimusPrime


Very late for this, but just want to share my updates on @ChoptimusPrime's answer. Not hard-coding the format:

ko.validation.rules['dateAndFormat'] = {
    validator: function (val, format) {
        return ko.validation.utils.isEmptyVal(val) || moment(val, format).isValid();
    },
    message: 'Please provide a valid date.'
};
ko.validation.registerExtenders();
like image 43
hofnarwillie Avatar answered Jan 01 '26 06:01

hofnarwillie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!