Book Image

KnockoutJS Essentials

By : Jorge Ferrando, Jorge F Ferrando
Book Image

KnockoutJS Essentials

By: Jorge Ferrando, Jorge F Ferrando

Overview of this book

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

Extending the customer model


You also need to validate the customer data. These are the rules for validation:

  • A first name is required

  • A last name is required and it needs a minimum of three characters

  • An address is required and it needs a minimum of five characters

  • An e-mail address is required and must match the built-in e-mail pattern

  • The zip code is required and must contain five numbers

To achieve this task, make some updates in the code, as follows:

  1. Extend the customer object in the models/Customer.js file:

    var firstName = ko.observable('').extend({
      required: true
    });
    var lastName = ko.observable('').extend({
      required: true,
      minLength: 3
    });
    var fullName = ko.computed(function(){
      return firstName() + ' ' + lastName();
    });
    var address = ko.observable('').extend({
      required: true,
      minLength: 5
    });
    var email = ko.observable('').extend({
      required: true,
      email: true
    });
    var zipCode = ko.observable('').extend({
      required: true,
      pattern: {
        message: 'Zip code should have 5 numbers...