我正在尝试使用来自 GitHub 的 Knockoutjs 验证插件。大多数似乎都可以正常工作,但是当我尝试使用扩展验证 mustEqual(password/confirm password) 时,它似乎什么也没做。我错过了什么?
我非常想学习这种扩展器技术以备将来使用。
(这整个 html 和 javascript 也通过 AJAX 调用加载到页面。如果这与它有任何关系。)
我的 javascript
function UserAccount(data) {
var self = this;
self.Password = ko.observable(data.Password).extend({ required: true, minlength: 6, message: "Password is required", maxLength: 12 });
self.Firstname = ko.observable(data.Firstname).extend({ required: true, minlength: 6, message: "Firstname is required", maxLength: 40 });
self.Lastname = ko.observable(data.Lastname).extend({ required: true, minlength: 6, message: "Lastname is required", maxLength: 40 });
self.Email = ko.observable(data.Email).extend({ required: true, minlength: 6, message: "Email is required", email: true, maxLength: 90 });
self.ConfirmPassword = ko.observable().extend({ mustEqual: self.Password()});
...........................Other Code............
}
ko.validation.rules['mustEqual'] = {
validator: function (val, otherVal) {
alert("Hello");
return val === otherVal;
},
message: 'The field must equal {0}'
};
$(document).ready(function () {
ko.applyBindings(new UserAccount(initdata), $("#UserAccount").get(0));
ko.validation.registerExtenders();
});