Alexandru Pupsa
Alexandru Pupsa

Reputation: 1878

Input number with comma isn't parsed as numeric value in the data-bind, in IE

<input type="number" data-bind="value: value"/>

<h2>Value: <span data-bind="text: value"/></h2>

var ViewModel = function() {   
this.value = ko.observable();
};

ko.applyBindings(new ViewModel()); 

Now, if I enter "23,45", in chrome the data-bind value will be saved as "23.45", but in Internet Explorer it will still be "23,45". Both browsers are in French and the Windows 10 I'm running is also set to French regional settings. French uses comma for decimal separator.

So how can I make IE bind the value properly? As numeric.

Upvotes: 1

Views: 261

Answers (1)

Bryan Dellinger
Bryan Dellinger

Reputation: 5304

how about just a writable computed observable http://knockoutjs.com/documentation/computed-writable.html

function model() {
  var self = this;
  this.mynumber = ko.observable();
  this.computedNumber = ko.pureComputed({
    read: function() {
      return this.mynumber()
    },
    write: function(value) {
      var n = value.replace(/,/g, ".");
      var number = (!isNaN(parseFloat(n)) && isFinite(n)) ? n : '';
      self.mynumber(number);
    },
    owner: this
  });

}

var mymodel = new model();

$(document).ready(function() {
  ko.applyBindings(mymodel);
});
<script src="http://knockoutjs.com/downloads/knockout-3.2.0.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input data-bind="value: computedNumber" />

Upvotes: 1

Related Questions