How to implement thousand seperator for text-boxes in ASP.NET C# (500 pt)

meetpd
meetpd used Ask the Experts™
on
Hi,

For textboxes having amount values, I need a thousand seperator.
Eg. If we enter amount as '15000', it should change it to '15,000'.

Also, because I have more than 50 such textboxes, I need a way to implement this in one shot.

Appreciate your help!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Rahul Goel ITILSenior Consultant - Deloitte

Commented:
double value;

value = 1234567890;
Console.WriteLine(value.ToString("#,#", CultureInfo.InvariantCulture));    
// Displays 1,234,567,890      

Author

Commented:
How can we apply this to all the textboxes?
two Solutions:

1> Implement a Ajax Mask Edit Extender (Out of the Box functionality) and attach it to ur text box ,a more professional approach as it will help u if tomorrow ur site switches for different language then ur Currency Format Will Change.

http://www.asp.net/AJAX/AjaxControlToolkit/Samples/MaskedEdit/MaskedEdit.aspx

2> A More easy approcah ,create a Custom Function to return a Number format

txtBox.text = ReturnFormat(NumToBeDisplayed);

string RetrunFormat(int Num)
{
  return Num.ToString("N");

}
txtBox.text = ReturnFormat(NumToBeDisplayed);
 
string RetrunFormat(int Num)
{
  return Num.ToString("N");
 
} 

Open in new window

Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Author

Commented:
Hi DreamsTech,

I tried the solution that you gave. But it inserts two 0s after decimal.(e.g. 13,000.00) How can I remove those so that I get (e.g. 13,000) ?

Thanks!

Author

Commented:
Also, I would want this to happen when the cursor is out of the textbox. How to do that?
Commented:
I suggest to use  java-script here. because you need this feature in client not server. don't you?
The code bellow is a java-script function that get a textbox( i.e. input tag in html) and make it a thousand separator.
you just need to add this attribute to every textbox:
onkeyup="javascript:moneyCommaSep(this);"
it could be done like this:
mytxtbx.Attribute.Add("onkeyup","javascript:moneyCommaSep(this);");
Also the ajax based solution is still usefull.( I mean MaskedEditBox)


function moneyCommaSep(ctrl)
50{
51 var separator = ",";
52 var int = ctrl.value.replace ( new RegExp ( separator, "g" ), "" );
53 var regexp = new RegExp ( "\\B(\\d{3})(" + separator + "|$)" );
54 do
55 {
56 int = int.replace ( regexp, separator + "$1" );
57 }
58 while ( int.search ( regexp ) >= 0 )
59 ctrl.value = int;
60} 

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial