Using JavaScript, I want to show up to two decimal places, a comma, and a comma after two integers, but I tried to show up to nine digits using integers, but I'm not good at showing two integers and two decimal places.
(like 1.50% or 50.00% or 99.99%...etc)
If enter more digits than that, I want to prevent it from entering.
// Thousand commas (including decimal point)
function numberWithCommas(num) {
var parts = num.toString().split(".");
return parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",") + (parts[1] ? "." + parts[1] : "");
}
//Number + - check (remove all non-numeric + - values) Up to 9 numbers
function chkNumber(obj) {
var tmpValue = $(obj).val().replace(/[^0-9,^\-]/g, '');
tmpValue = tmpValue.replace(/[,]/g, '');
tmpValue = tmpValue.substring(0, 9);
// Forced value change after processing thousands commas
obj.value = numberWithCommas(tmpValue);
}
// Remove anything other than numeric values
function chkrealNumber(obj) {
var tmpValue = $(obj).val().replace(/[^0-9,]/g, '');
obj.value = tmpValue.replace(/[,]/g, '');
}