I am using knockout validation and here is how my model looks like
function SignInViewModel() {
var self = this;
self.userName = ko.observable('').extend({
required: true,
pattern: {
message: 'Username must be a valid email address',
params: /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/
}
});
self.password = ko.observable('').extend({
required: true
});
self.errors = ko.validation.group(self);
self.login = function (e) {
if (self.errors().length == 0) {
$.post("/account/jsonlogin", { userName: self.userName(), password: self.password(), returnUrl: "/" })
.done(function (result) {
redirect(result.redirect);
}).error(function () {
});
} else {
self.errors.showAllMessages();
}
}
}
ko.validation.configure({
insertMessages: true,
decorateElement: true,
errorElementClass: 'error'
});
$(function () {
ko.applyBindings(new SignInViewModel());
});
When I put an invalid email address in userName
and call login by clicking my submit button then knockout appends a error message span right next to element but it's set to display none. What is the problem.
Thanks