javascript 如何设置文本框的货币格式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16209599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 03:42:45  来源:igfitidea点击:

How to set currency format for textbox?

javascriptasp.net

提问by Niloo

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

我想在文本框中输入数字,文本框会自动将这些数字转换为货币。(12,345,654)

I can use FilteredTextBoxExtender

我可以使用 FilteredTextBoxExtender

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

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

但我想在用户输入数字时自动添加逗号。

回答by Niloo

I use javascript code.

我使用 javascript 代码。

  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, '' + ',' + '');
        return x1 + x2;
    }


<asp:TextBox ID="aPriceTextBox" runat="server" Width="100px"    onkeyup = "javascript:this.value=Comma(this.value);" />

回答by Vivek Jain

Using a masked-edit controlwould be a good idea here.

在这里使用屏蔽编辑控件将是一个好主意。

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

查看StackOverflow 上的这篇文章了解更多信息。它建议了为货币字段实现文本框的方法。

You may also refer to this componenton codeplex.

你也可以在 codeplex 上参考这个组件

回答by Mithrand1r

You can use ajax maskedit extander
here is some example:

您可以
在此处使用 ajax maskedit extander, 这是一些示例:

<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" />