Search code examples
knockout.jsknockout-validation

Knockout Validation min/max validation with commas in value


I have the following fields in my ViewModel:

self.agi = ko.observable("")
        .extend({ required: true, min: .01 });

But when I enter a value of 1,000 I get the following error:

Please enter a value greater than or equal to 0.01.

1000 validates properly. How can I make the validation disregard the comma?


Solution

  • Custom validation for min/max validation with commas in value.

    ko.validation.rules['minCheck'] = {
      validator: function(val, min) {
          val=val.replace(/\,/g,'');
          return ko.validation.utils.isEmptyVal(val) || val >= min;
        },
     message: 'Please enter a value greater than or equal to {0}.'
    };
    

    Fiddle Demo