Your custom verification code is OK. But you are not correctly calling the ko.validation.registerExtenders(); method ko.validation.registerExtenders(); .
Although this is not explicitly stated, you need to call ko.validation.registerExtenders(); before , you call ko.applyBindings .
So, to fix your code, you just need to write:
$(document).ready(function () { ko.validation.registerExtenders(); ko.applyBindings(new UserAccount(initdata), $("#UserAccount").get(0)); });
But you will encounter another problem: the mustEqual validator mustEqual designed to compare with static values, so it will not work if you want to compare two properties, such as password with password confirmation.
What you need is something like the user who contributed the βSameβ validator :
ko.validation.rules['areSame'] = { getValue: function (o) { return (typeof o === 'function' ? o() : o); }, validator: function (val, otherField) { return val === this.getValue(otherField); }, message: 'The fields must have the same value' };
What you can use, for example:
self.Password = ko.observable(data.Password).extend({ required: true, minlength: 6, message: "Password is required", maxLength: 12 }); self.ConfirmPassword = ko.observable().extend({ areSame: self.Password });
JSFiddle demo.
nemesv
source share