Mike Cole
Mike Cole

Reputation: 14763

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?

Upvotes: 0

Views: 4306

Answers (1)

Akhlesh
Akhlesh

Reputation: 2399

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

Upvotes: 1

Related Questions