Using jquery, how do you automatically fill out a numeric form input with a comma?

I am new to jquery and cannot figure out how to do this. Basically, when entering a large number in a form text box, I would like the commas to be automatically placed in the field for the user, without having to enter them themselves. Can anyone help me figure this out?

Thank.

+2


source to share


4 answers


I think you want to have some kind of hidden input, install this plugin.



+3


source


You can use number formatting plugin

here are some examples from the plugin page (link provided above):



  // Brazilian currency format
  $(".numeric").number_format({precision: 2, decimal: ',', thousands: '.'});

  /* Results: number are formatted as they are typed
  123,45
  1.234,56*/
  // Changing precision to 5 decimal digits
  $(".numeric").number_format({precision: 5});

  /* Results: number are formatted as they are typed
  1,234.56789 
  0.12345 */

      

+2


source


At the top of my head, fields cannot be entered, they have a format mask on them If not, and someone has a gun in my head and said, do it now, I would take the full length of the cavernous number data storage and then divide it by 3, I know how many commas I need. Then, using javascript, use the substring method to grab all three from the right and place a comma in front of it.

0


source


Use a blur event to reformat the number when the user leaves the text box.

$(".selector").blur(function() {
  $(this).val() = commafyValue($(this).val());
}

      

I missed the cubism function from here , but there is something to choose from or you can write your own ...

function commafyValue(nStr)
{
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}

      

0


source







All Articles