Knockout date validation not working correctly

前端 未结 3 1479
粉色の甜心
粉色の甜心 2021-01-22 03:54

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://g

3条回答
  •  孤城傲影
    2021-01-22 04:35

    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 });
    

提交回复
热议问题