Search code examples
javascriptasp.net-coremodel-view-controllerviewseparator

Adding point as thousand separator, but it adds point after each number point


hello I must have point as a thousand separator. If you type in numbers in the input field, it has to be seen live. The data type is integer. The problem is that the function adds the dots after each number.

function numberDots(x) {
        return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ".");
    }

<div class="form-group">
                                                
       <input type="text" asp-for="inputfield" class="form-control  text-right" onkeyup="this.value=numberWithDots(this.value);"/>
       <span  asp-validation-for="inputfield" class="text-danger"></span>
</div>

this is my code. I want this: 222.222 but I get this: 2.2.2.222

Can you please help me?


Solution

  • Change your js like below:

    function numberWithDots(x) {         
        return x.toString().replace(/^0+/, '').replace(/\D/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ".")            
    }