1

I want enter numbers into textbox and textbox would convert automatically these number into currency.(12,345,654)

I can use FilteredTextBoxExtender

<ajaxToolkit:FilteredTextBoxExtender ID="ftbe" runat="server"
TargetControlID="TextBox3"         
FilterType="Custom, Numbers"
ValidChars="," />

But i want to automatically add commas when user enters number.

Niloo
  • 1,205
  • 5
  • 29
  • 53

3 Answers3

6

I use javascript code.

  function Comma(Num) { //function to add commas to textboxes
        Num += '';
        Num = Num.replace(',', ''); Num = Num.replace(',', ''); Num = Num.replace(',', '');
        Num = Num.replace(',', ''); Num = Num.replace(',', ''); Num = Num.replace(',', '');
        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;
    }


<asp:TextBox ID="aPriceTextBox" runat="server" Width="100px"    onkeyup = "javascript:this.value=Comma(this.value);" />
Niloo
  • 1,205
  • 5
  • 29
  • 53
0

Using a masked-edit control would be a good idea here.

Check this post on StackOverflow for more information. It suggests ways to implement a textbox for currency field.

You may also refer to this component on codeplex.

Community
  • 1
  • 1
Vivek Jain
  • 3,811
  • 6
  • 30
  • 47
  • Thanks, but i don't want to use this because i do not want to limit the number of characters. – Niloo Apr 25 '13 at 08:46
  • @ARG this being currency value, I'd suggest to put a limit on the no. of characters. 12,345,678,910.00 is a huge no. Whatever suits your purpose an input string with 20-30 characters is also fine, but there should be a limit. You would not like users entering a number whose length is 100 characters. – Vivek Jain Apr 25 '13 at 08:49
  • Thanks, i should set mask for this (Mask="9,999,999.99"),i want to add commas when user enter number.Are there any other solutions? – Niloo Apr 25 '13 at 08:54
  • This should work just fine for you. Just set the mask with commas. Rest would be taken care of by the control. Please accept this as an answer if it has helped you, so that other visitors could find a solution quickly. Thanks! – Vivek Jain Apr 25 '13 at 13:49
0

You can use ajax maskedit extander
here is some example:

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <asp:TextBox ID="TextBox1" runat="server" />
        <cc1:MaskedEditExtender runat ="server"
            TargetControlID="TextBox1"
            Mask="999,999,999,999"
            MessageValidatorTip="true"
            MaskType="Number"
            InputDirection="RightToLeft"
            AcceptNegative="Left"
            DisplayMoney="None"
            ErrorTooltipEnabled="True" />
Mithrand1r
  • 2,313
  • 9
  • 37
  • 76
  • Thanks, but I do not want to limit the number of characters. – Niloo Apr 25 '13 at 08:44
  • I tried this exact code but the second I type a "-" for a negative number the textbox just reads "NaN". I haven't been able to find a solution to this. Have you seen this before? @Niloo – Taylor Brown Jan 14 '14 at 17:05