fatiDev
fatiDev

Reputation: 5992

Replace dot character once entered by a comma in jquery

I need to replace dot character on entered by the user by the comma . I wrote this code but it doesn't give the hopped result

$(".dot").keyup(function (event) {

     val = $(this).val();
     length = val.length;
     if (event.key == '.') {
         event.stopPropagation();
         $(this).val(val.substring(0, length)+",");
     }
 });

Upvotes: 0

Views: 2090

Answers (2)

EnriqueDev
EnriqueDev

Reputation: 1247

have you tried using the string method replace?

var res = str.replace(".", ",");

Upvotes: 3

Kunal Patel
Kunal Patel

Reputation: 119

Please try this.

$('input[type = "text"]').on("keyup", function (e) {
		var val = $(this).val();
    var str=val.replace('.',',');
    $(this).val(str);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='text' id='txtVal' />

Upvotes: 3

Related Questions