敲门日期验证无法正常工作 [英] Knockout date validation not working correctly
问题描述
我需要以本地化格式(SK)验证日期,并且无法进行敲门验证。
I need to validate date in localized format (SK) and it is not possible with knockout validation.
我正在使用:
Durandal 1.2.0
Knockout 2.3.0
Knockout验证 https://github.com/ Knockout-Contrib / Knockout-Validation
我需要这样的工作:
var newStartDate = ko.observable()
.extend({
required: { message: 'Start date is required.' },
date: true,
formattedDate: { format: 'ddMMyyyy', delimiter: '.' }
});
然后如果我调用newStartDate.isValid()我得到这些结果:
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)
结果应该是:
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.
推荐答案
为什么不...
日期规则对我的用途来说似乎有点太宽容了。如果你喜欢时刻,但要为呼叫者提供更干净的内容,请将您的规则添加到validation.rules对象中:
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'
};
然后使用它:
var someDate= ko.observable().extend({ simpleDate: true });
这篇关于敲门日期验证无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!