Knockout validation: Сomparison of the two fields
February 04, 2013
- Categories
- JavaScript
- Knockout
- Programming
I would like to share how I've solved this problem.
The typical example is a simple registration form with the view model:
var viewModel = ko.validatedObservable({
userName: ko.observable().extend({ required: true }),
password: ko.observable().extend({ required: true }),
passwordConfirm: ko.observable().extend({ required: true })
});
Then you can easily check is your viewModel valid using:
viewModel.isValid();
And it works very good. But you don't have validation are your password and password confirmation fields equal.
To do it you have to add these lines:
viewModel().password.extend({ equal: viewModel().passwordConfirm });
viewModel().passwordConfirm.extend({ equal: viewModel().password });
After that you will have client notification as well as ability to check is your view model valid in code (as mentioned above).
That's all.
Good luck.
Recommended content
-
JavaScript modularity with RequireJS (from spaghetti code to ravioli code)
November 14, 2012
-
Use Serilog with Minimal API in .NET 6
November 19, 2021
-
C# tutorial for beginners | 2 | Write your first C# program
March 06, 2021
-
C# tutorial for beginners | 1 | Setup development environment
February 17, 2021
-
Localizable text template engine using StringTemplate 4
April 23, 2012
-
Localizable text template engine using RazorEngine
April 19, 2012