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.

2
  • 1
    jsfiddle.net/CBDea/1 check this link by Scott Mitchell
    – MMK
    Apr 25, 2013 at 8:28
  • Thanks, but i want add commas when user enter number.
    – Niloo
    Apr 25, 2013 at 8:45

3 Answers 3

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);" />
2
  • thats what i need. thnx
    – TheMuyu
    Nov 13, 2014 at 9:34
  • What about cents ??
    – M_R_K
    May 9, 2017 at 6:04
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.

4
  • Thanks, but i don't want to use this because i do not want to limit the number of characters.
    – Niloo
    Apr 25, 2013 at 8: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, 2013 at 8: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, 2013 at 8: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, 2013 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" />
2
  • Thanks, but I do not want to limit the number of characters.
    – Niloo
    Apr 25, 2013 at 8: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 Jan 14, 2014 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.