Book Image

Mastering KnockoutJS

By : Timothy Moran
Book Image

Mastering KnockoutJS

By: Timothy Moran

Overview of this book

Table of Contents (16 chapters)
Mastering KnockoutJS
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Knockout Validation


The validation of user input is a common enough task that nearly every web application will have at least some need for. By far the most popular Knockout plugin, with 50 percent more stars on GitHub than the next Knockout related project, Knockout Validation creates several extenders and binding handlers that are designed to simplify HTML form validation.

The use of the plugin starts with extenders that apply validation logic to observables without replacing them:

var requiredValue = ko.observable().extend({ required: true });
var multipleValidationValue = ko.observable().extend({
                     required: true,
                     minLength: 3,
                     pattern: {
                          message: 'Hey this doesnt match my pattern',
                          params: '^[A-Z0-9].$'
                     }
                 });

Binding against validation-extended values is done with the normal value binding:

<input data-bind="value: requiredValue" />

Knockout...