0

When I'm texting the number in my text (or number) field I need the number to be divided by thousands with a comma.

Example

 1. instead of it saying “1000” make it say “1,000”.
 2. “1000000” make it say “1,000,000”.
Sumit patel
  • 3,807
  • 9
  • 34
  • 61
Morgari
  • 524
  • 2
  • 8
  • 24
  • This link could be of some help to you http://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript – Geeky Nov 09 '16 at 06:06

3 Answers3

4

Using Script

$(document).on('keyup', '.Amount', function() {
    var x = $(this).val();
    $(this).val(x.toString().replace(/,/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ","));
});

Live Demo Here

Snippet Example Below

$(document).on('keyup', '.Amount', function() {
    var x = $(this).val();
    $(this).val(x.toString().replace(/,/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ","));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
Enter Amount:<input class="Amount" type="Amount" />
</form>
Sumit patel
  • 3,807
  • 9
  • 34
  • 61
2

This could help you

window.onload=function(){
  var numberinput=document.getElementById('number');
  numberinput.addEventListener('change',changedValue);
}

function changedValue(){
  var input=this.value
  var number=parseInt(input,10);
  if(number>=1000)
    {
      
    var numbersArray = input.toString().split(".");
    numbersArray[0] = numbersArray[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    this.value= numbersArray.join(".");
    }
}
<input type="text" id="number" >

Hope this helps

Geeky
  • 7,420
  • 2
  • 24
  • 50
1

 $('input.number').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, ",")
        ;
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
    <input class="number">

JS Fiddle

Daniel J Abraham
  • 234
  • 2
  • 12