To show error message after validating the observable object, you can do the following:
var ViewModel = function() {
var self = this;
self.myInteger = ko.observable().extend({ validation: "Please pass numerical value that is less than 255" });
}
ko.extenders.validation = function (target, overrideMessage) {
target.hasError = ko.observable();
target.validationMessage = ko.observable();
function validate(newValue) {
// write your validation here
// check if it is numerical
// check if it is less than the max value
var passTheValidation = true;
target.hasError(!passTheValidation);
target.validationMessage(passTheValidation ? "" : overrideMessage || "This failed the validation");
}
//initial validation
validate(target());
//validate whenever the value changes
target.subscribe(validate);
//return the original observable
return target;
}
Then show the error message like this
<div>
<input data-bind='value: myInteger, valueUpdate: "afterkeydown"' />
<span data-bind='visible: myInteger.hasError, text: myInteger.validationMessage'> </span>
</div>
There is a good reference in the website regarding this extenders http://knockoutjs.com/documentation/extenders.html