Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ko validation Maxlength only if

Tags:

knockout.js

For knockout required validation, the onlyIf is used in this way. How about maxLength validation? I have tried as follow but not working. Hope someone could help on this.

self.postalCode.extend({
  required: {
    message: "Postal Code is required",
    onlyIf: function() {
      return self.noPostalCode() === false;
    }
  }
});

// Not Working
self.postalCode.extend({
  maxLength: 3 {
    message: "Postal Code is required",
    onlyIf: function() {
      return self.noPostalCode() === false;
    }
  }
});
like image 522
vincentsty Avatar asked May 14 '26 03:05

vincentsty


1 Answers

All you need to do is set maxLength property value using params

viewModel:

self.postalCode.extend({
      maxLength: {
          message: "Postal Code is required",
          onlyIf: function () {
              return self.noPostalCode() === false;
           },
         params: 3
      }
 });

working sample here

like image 109
super cool Avatar answered May 17 '26 07:05

super cool



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!