2013-04-25 57 views
1

我想輸入數字到文本框和文本框將這些號碼會自動轉換成貨幣。(12345654)如何設置文本框的貨幣格式?

我可以使用FilteredTextBoxExtender

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

但我想,當用戶輸入號碼自動添加逗號。

+1

http://jsfiddle.net/CBDea/1/檢查此鏈接Scott Mitchell – MMK 2013-04-25 08:28:28

+0

謝謝,但我想在用戶輸入數字時添加逗號。 – Niloo 2013-04-25 08:45:29

回答

5

我使用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, '$1' + ',' + '$2'); 
     return x1 + x2; 
    } 


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

這就是我需要的。 thnx – TheMuyu 2014-11-13 09:34:58

+0

我用你的解決方案,像魅力一樣工作。 – Amrik 2015-08-04 22:48:40

+0

怎麼樣? – 2017-05-09 06:04:44

0

使用masked-edit control會在這裏是個好主意。

在計算器上檢查this post以獲取更多信息。它提出了實現貨幣字段文本框的方法。

您也可以參考this component CodePlex上。

+0

謝謝,但我不想用這個,因爲我不想限制字符數。 – Niloo 2013-04-25 08:46:34

+0

@ARG這是貨幣價值,我會建議限制在沒有。的字符。 12,345,678,910.00是一個巨大的號碼。無論你的目的是什麼,20-30個字符的輸入字符串也很好,但應該有一個限制。您不希望用戶輸入長度爲100個字符的數字。 – 2013-04-25 08:49:16

+0

謝謝,我應該爲此設置掩碼(Mask =「9,999,999.99」),我想在用戶輸入數字時添加逗號。是否有其他解決方案? – Niloo 2013-04-25 08:54:21

0

您可以使用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" /> 
+0

謝謝,但我不想限制字符的數量。 – Niloo 2013-04-25 08:44:19

+0

我嘗試了這個確切的代碼,但第二個我輸入「 - 」爲負數的文本框只是讀「NaN」。我一直無法找到解決辦法。你以前見過這個嗎? @Niloo – 2014-01-14 17:05:57

+0

我使用http://stackoverflow.com/a/16260138/1825829,對我來說沒問題。 – Niloo 2014-01-14 19:33:31