Add Comma automatically while entering amounts in textbox using Javascript
Use following JavaScript function to add comma in numeric input in textbox
Use following event of input textbox to call above function
function Comma(Num)
{
Num += '';
Num = Num.replace(/,/g, '');
x = Num.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d)((\d)(\d{2}?)+)$/;
while (rgx.test(x1))
x1 = x1.replace(rgx, '$1' + ',' + '$2');
return x1 + x2;
}
Use following event of input textbox to call above function
onkeyup = "javascript:this.value=Comma(this.value);"
This doesn't work as it should.
ReplyDeleteIt works ok for the first thousand - a comma is inserted after entering 1000 - but when you continue entering numbers after that, it inserts consecutive commas after 2 digits, not 3, so you get something like this: 10,00,000 - where it should be: 1,000,000
Did you test this with more than 4 digits?
Thank you for your valuable comment
Deletei will look into the issue and update this post soon
just replace the regular expression with this:
Deletevar rgx = /(\d)((\d{3}?)+)$/;
in c#
ReplyDeletei need in asp.net textbox when type amount 12000 out should come in 12,000 likes this
ReplyDelete