0

I have a dynamic table with input fields with type = number, I want the jquery to format all the input on page load with type = numbers to number format (1,000,000) for 3 value place then it will add a comma.

I have tried using number.js and google visualization and jquery number format but I cant find the one that is working properly. I want the simplest way to do it.

The document should load with the inputs type numbers formatted, I tried so many codes but none is working :((

i also tried this one 1

Community
  • 1
  • 1
SCS
  • 1,162
  • 3
  • 17
  • 31
  • after page loading finished, u can do this,or u can format on server side. – sakir Jun 28 '16 at 17:04
  • You can't format the value in [an input with `type="number"`](https://www.w3.org/TR/html-markup/input.number.html). The implementation of the control is based on the browser/OS. – Heretic Monkey Jun 28 '16 at 21:33

1 Answers1

0

Here is a simple solution I have used before, it uses regular expressions.

$(document).ready(function(){
    $('button').on('click', function(){
        $(".format").each(function(){
            var str = $(this).text().split('.');
            str[0] = str[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
            $(this).text(str[0] + "." + str[1]); 
        });
    });
});

Have a look at this post for an explanation of the regex used.

Community
  • 1
  • 1
Courtney
  • 309
  • 2
  • 10
  • Pretty sure this won't work because a) for input elements, you'd use `val()` rather than `text()` and b) the type of the input is "number", so setting the value to anything parseable as a number just sets the value to that numeric value, not the formatted one. – Heretic Monkey Jun 28 '16 at 21:40