0

I have a input text field which users can input a number and for a currency.

<input type="text" name="com-uprice" value="">

I used this snippet to add a comma for every 3 digits

        $('input[name="com-uprice"]').keyup(function(event) {

          // skip for arrow keys
          if(event.which >= 37 && event.which <= 40) return;

          // format number
          $(this).val(function(index, value) {
            return value
            .replace(/\D/g, "")
            .replace(/\B(?=(\d{3})+(?!\d))/g, ",");
            console.log(this.val)
          });
        });

Now I need that value into a number for computation purposes I used this line of code to convert it into a number

parseInt($('input[name="com-uprice"]').val().replace(',', ''))

It returns a number but not the whole number that users inputted.

For example. If I input 1,000,000 It returns like 1000 only

John Wick
  • 17
  • 7

1 Answers1

0

Replace function only replaces first occurance of the given string. You can use below snippets.

parseInt($('input[name="com-uprice"]').val().split(',').join(''))

parseInt($('input[name="com-uprice"]').val().replace(/,/g, '')

(Starting from chrome v85)

parseInt($('input[name="com-uprice"]').val().replaceAll(',', '')
Taha Metin Bayi
  • 201
  • 1
  • 8