问题描述
我有一个 asp.net mvc3 项目,我在其中使用淘汰绑定对表格进行了批量编辑.我想在保存数据的同时进行诸如必需和数字验证之类的验证.有没有更简单的方法来进行淘汰验证.PS:我没有使用表格.
I have asp.net mvc3 project where I do a bulk edit on a table with knockout binding. I want to do validations like required and number validations while saving data. Is there any easier way to do knock out validations.PS: I am not using forms.
推荐答案
看看 Knockout-Validation设置和使用 knockout 文档 中描述的内容.下:现场示例 1:强制输入为数字
Have a look at Knockout-Validation which cleanly setups and uses what's described in the knockout documentation. Under: Live Example 1: Forcing input to be numeric
更新:fiddle 已更新为使用最新的 KO 2.0.3 和 ko.validation 1.0.2,使用 cloudfare CDN url
UPDATE: the fiddle has been updated to use the latest KO 2.0.3 and ko.validation 1.0.2 using the cloudfare CDN urls
设置ko.validation:
To setup ko.validation:
ko.validation.rules.pattern.message = 'Invalid.';
ko.validation.configure({
registerExtenders: true,
messagesOnModified: true,
insertMessages: true,
parseInputAttributes: true,
messageTemplate: null
});
要设置验证规则,请使用扩展程序.例如:
To setup validation rules, use extenders. For instance:
var viewModel = {
firstName: ko.observable().extend({ minLength: 2, maxLength: 10 }),
lastName: ko.observable().extend({ required: true }),
emailAddress: ko.observable().extend({ // custom message
required: { message: 'Please supply your email address.' }
})
};
这篇关于敲除验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!