Using jquery, how do you automatically fill out a numeric form input with a comma?
4 answers
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 to share
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 to share
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 to share