Javascript replacing a dot instead removes everything

  • A+
Category:Languages

i've walked into a strange problem. When trying to replace a dot on a number input, instead of replacing just that dot, it clears out the entire input.

$("[data-input-payment-id]").on("keyup", function(e) {   var test_value = $(this).val().replace(//./g, "");   $(this).val(test_value); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="number" data-input-payment-id="12">

JSFIDDLE

How to change it so it only removes the dots?

Thanks everyone

 


This isn't really a solution as i'd personally like to see it, but here is what i did to solve the problem at hand.

i changed the javascript to listen for keycode 46 (the ".") and im returning false on the paste event listener

Updated

$("[data-input-payment-id]").on("keypress", function (e) {          var key = e.charCode ? e.charCode : e.keyCode;          if (key == 46) {              return false;          }          });      $("[data-input-payment-id]").on("paste", function (e) {         return false;      });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="number"data-input-payment-id="12">

});

Hope this helps anyone else having this problem. Works atleast in chrome and edge.

~Greetings

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: