Hi dear,
Now I using bellow script for format numbers.
it is small and better than ... tested.
with special thanks to my dear friend Mr.Ali Mostafavi.
using example:
.numeric cssClass for just numbers.
.numericAmount cssClass for numbers with thousands separator.
.numericAmountDecimal cssClass for numbers with decimal and thousands separator.
<script tag start
```
$(document).ready(function () {
$(".numeric").on('keyup', function () {
this.value = this.value.replace(/[^0-9]/, '');
$(this).val(this.value);
});
$(".numericAmount").on('keyup', function () {
this.value = this.value.replace(/[^0-9]/g, '');
$(this).val(ReplaceNumberWithCommas(this.value));
});
$(".numericAmountDecimal").on('keypress', function (e) {
var nkeycode = e.charCode || e.keyCode;
var chDecimalDot = String.fromCharCode(nkeycode).toLowerCase();
if (chDecimalDot === ('.').toLowerCase()) {
var dotArray = this.value.toString().split('.');
if (dotArray.length > 1) {
return false;
}
else if (this.value == 0) {
this.value = '0.';
return false;
}
}
});
$(".numericAmountDecimal").on('keyup', function (e) {
this.value = this.value.replace(/[^0-9\.]/g, '');
$(this).val(ReplaceNumberWithCommas(this.value));
});
});
function ReplaceNumberWithCommas(yourNumber) {
//Separates the components of the number
var n = yourNumber.toString().split(".");
//Comma-fies the first part
n[0] = n[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
//Combines the two sections
return n.join(".");
}
```
script tag end>