I have been looking around and can't seem to find an way to do this. As a user inputs a number into a input field e.g:
<input type="text" class="numberOnly" id="miles" value="" />
How can I get that to update as they are typing to something like. e.g:
Input: 100000
Output: 100,000
I have some similar code that formats some other things as I calculate them, here is the function I'm using to format my other numbers.
function format_num(number) {
number += '';
x = number.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;
}
This works fine, like I said I'm not sure how to put the users input value into this and get the output back into the input field while they are typing.
Note: I need to format more then 1 input field.
Update:
I have this in my code that will trigger each time the keyboard is pressed, so that can be used. This is used for the whole form so the form updates each time a button is pressed.
$(".numberOnly").keypress(function (e) {
//if the letter is not digit then display error and don't type anything
if (e.which != 8 && e.which != 46 && e.which != 0 && (e.which < 48 || e.which > 57)) {
//display error message
$("#errmsg").html("Digits Only").show().fadeOut(1600);
return false;
} else {
$('input').keyup(function () {
});
}
});
Update 2:
Is it possible to make an kind of overlay for it, e.g: the value of the input is 1000 but the overlay shows it to the use as 1,000?
Any help would be great guys. Many thanks!