Search code examples
javascriptknockout.jsknockout-validation

Customize required = true in knockout validation


HTML:

<input class="form-control" type="text" data-bind="value: Participant().FirstName />

Model:

self.FirstName = ko.observable(data === null ? "" : data.FirstName);

ViewModel:

ko.validation.init();
     self.ApplyValidations = function () {
                    var participant = new Participant(null);
      participant.FirstName.extend({
                        required: true
                    }),

The validation is not working, maybe because I already have 0 or "" passed in text fields. Can anyone help me with customizing the required so that it avoids "" or 0 and validates the textbox?


Solution

  • Try this in your Model instead.

     self.FirstName = ko.observable(self.FirstName || "")