user11535870
user11535870

Reputation:

javascript number formatting on keyup event

I am trying to format number in an input field on keyup event but I kept getting a warning on the console of my browser The specified value "5,545" cannot be parsed, or is out of range. and the value I have entered in the input field cleans up. Please what can be done to solve the problem?
Below is the jquery code snippet that handles the number formatting:

$('input[type="number"]').keyup(function(event) {
        // skip for arrow keys
        if(event.which >= 37 && event.which <= 40){
            event.preventDefault();
        }
        $(this).val(function(index, value) {
            return value
            .replace(/\D/g, "")
            .replace(/([0-9])([0-9]{2})$/, '$1.$2')  
            .replace(/\B(?=(\d{3})+(?!\d)\.?)/g, ",");
        });
    });


Error or warning I get in the browser console is:
enter image description here
and the value in my input field cleans up after displaying this warning. Please what is wrong and what can be done?

Upvotes: 2

Views: 4255

Answers (1)

maassim
maassim

Reputation: 46

What causes the error is the , since 5,289 try changing the , to a .

EX: '5,289' => '5.289'

Upvotes: 2

Related Questions