#Knockout Validation A KnockoutJS Plugin for model and property validation
Contributors:
License: MIT
Tested in IE 6+, FF7, Chrome 15 ##Getting Started
//setup the validation library, you can optionally pass in an 'options' object to configure the plug-in
ko.validation.init();
//start using it!
var myValue = ko.observable().extend({ required: true });
//oooh complexity
var myComplexValue = ko.observable().extend({
required: true,
minLength: 3,
pattern: {
message: 'Hey this doesnt match my pattern',
params: '^[A-Z0-9].$'
}
});
//or chaining if you like that
var myComplexValue = ko.observable()
myComplexValue.extend({ required: true })
.extend({ minLength: 3 })
.extend({ pattern: {
message: 'Hey this doesnt match my pattern',
params: '^[A-Z0-9].$'
}});
//want to know if all of your ViewModel's properties are valid?
var myViewModel = ko.validatedObservable({
property1: ko.observable().extend({ required: true }),
property2: ko.observable().extend({ max: 10 })
});
console.log(myViewModel.isValid()); //false
myViewModel().property1('something');
myViewModel().property2(9);
console.log(myViewModel.isValid()); //true
see more examples on the Fiddle: https://jsfiddle.net/ericbarnard/KHFn8/
##Native Validation Rules Required:
var myObj = ko.observable('').extend({ required: true });
Min:
var myObj = ko.observable('').extend({ min: 2 });
Max: