Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,
I have a javascript function whicf I call on onkeyup event of textbox.
It has a regular expression that will format the values input by user with commas (,);
Problem with it is that, it puts comma after every third digit before decimal point.
I want it to put comma for first three digit before decimal point and then after every two digits (like we show amount).
12,34,567.8901

but it is showing like
1,234,567.8901

here is my script function :
JavaScript
function Comma(Num) 
{
   Num += '';
   Num = Num.replace(/,/g, '');

   x = Num.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;
} 


I think it has to do with that regular expression in function.
I tried using MaskedEditorExtender but client is not happy with it.
So please help me friends.

Any help appreciated.
Thanks,
Lok..
Posted
Updated 5-Dec-11 20:29pm
v2
Comments
[no name] 6-Dec-11 2:29am    
EDIT: added code block
Derek Henderson 6-Dec-11 5:18am    
I think your better off doing it in code without regular expressions. From what I can see your only using the RegExp to split the sting into 3's wring a small bit of javascript to split it using string functions not RegExp, its not really what RegExp was design for, and in my view over complicates what is a simple function.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900