Knockout validation and error handling module.
npm install --save knockout-validation
In your JavaScript code
require(['knockout', 'knockout-validation'], function (ko, koValidation) {
var ob = ko.observable().extend({
validate: [
// a list of validators
koValidation.string(),
]
});
ob('foo'); // this will success
ob(123); // this will fail
});
var ko = require('knockout');
var koValidation = require('knockout-validation');
var ob = ko.observable().extend({
validate: [
// a list of validators
koValidation.string(),
]
});
ob('foo'); // this will success
ob(123); // this will fail
Create a string validator verifies if the observable has a string value.
Create a string validator with limited length.
Create a string validator with XSS (cross site scripting) check.
Create a number validator verifies if the observable has a number value.
Create a number validator with limited integer part length and decimal part length.
Create a number validator with a range.
Create an enum validator validates if the observable has a value in the given value set.
Create an array validator validates if the observable has an array value.
Create an array validator with size validation.
Create an array validator with validators for every item.
validators
, array of validators, every validator should be passed by every item.
Create an array validator with validators which should be all passed one by one by every item.
validators
, array of validators array, every validators array should be passed by item with same index.additionalValidators
, boolean or array of validators. true means allow additional items for which there're no validation; false means no additional items are allowed; array of validators means every additional items should pass all these validators.
Create an array validator with no duplicate item validation.
Create a validator verifies if the observable has a value. If allowSpace
is
false, space only strings are treated as invalid.
Create a validator which sets observable's errors passively.
Set passive error to the observable. Error will be cleared when value changes.
message
, the passive error messagevalue
, associated value, if the observable is set to any other value, the error will be cleared
Create a validator using customized validation method.
isValid
, the validation method in form offunction (value) { return true; }
message
, the error message for validation failureblockInput
, a boolean decides whether invalid value will be blocked. If it'strue
, setting the observable with an invalid value will turn into a noop. Otherwise, the observable will be updated with themessage
added to the itserrors
property
Validate a value against an array of validators manually. Returns the error message of the first failed validator
For each validator, you can call its set
method to config the following
options
blockInput
, whether or not the invalid values are blocked from setting to the observablemessage
, the error message on validation failure
Validate a given value directly. Returns the error message if failed, otherwise
returns undefined
.