最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Knockout date validation not working correctly - Stack Overflow

programmeradmin6浏览0评论

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

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.

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./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.

Share Improve this question edited Dec 1, 2016 at 4:30 Alexandra 4,8533 gold badges27 silver badges34 bronze badges asked Aug 23, 2013 at 14:21 user2711254user2711254 391 silver badge3 bronze badges 2
  • Where does the formattedDate validation rule e from? I don't see it anywhere in the list of built-in or user-contributed custom rules in the Knockout-Validation docs. – oddurad Commented Aug 23, 2013 at 23:39
  • I found it here, but I am not 100% sure if its in my version, github./neildonkin/Knockout-Validation/mit/… – user2711254 Commented Aug 26, 2013 at 7:25
Add a ment  | 

3 Answers 3

Reset to default 13

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

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

The only working solution I have for now is below. The problem with this validator is that it also validates default(EN) date format as valid, so I have to add a IF to return this as invalid date format.

var dateValidator = function (val) {
            if (!val)
                return false;
            if (moment(val, 'DD.MM.YYYY HH:mm').isValid()) {
                return true;
            }
            else
                return false;
        };

var startDate = ko.observable().extend({ validation: { validator: dateValidator, message: 'Start of task is not in correct format.' } });

发布评论

评论列表(0)

  1. 暂无评论