本文介绍了汽车在用数字文本框中添加逗号(,)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我现在的编码加逗号(,)当有4个或多个数字。但不是
1101 = 1,101我的code是这样做的
1101 = 110,1 ...我希望它格式化,并把逗号前面。我的javascript
My current coding adds comma (,) when there are 4 or more digits. But instead of1101 = 1,101 my code is doing this1101 = 110,1... I want it to format and bring the comma to the front. My javascript
<script language="javascript" type="text/javascript">
function AddComma(txt) {
if (txt.value.length % 4 == 3) {
txt.value += ",";
}
}
</script>
<asp:TextBox ID="TextBox3" onkeypress="AddComma(this)" runat="server"></asp:TextBox>
我如何格式化所以逗号在前面1,101或10,101或100,101。
How do i format so the comma is in the front 1,101 or 10,101 or 100,101.
推荐答案
试试这个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="TextBox3" runat="server" onkeyup = "javascript:this.value=Comma(this.value);" />
希望这有助于你。
Hope this helps you.
这篇关于汽车在用数字文本框中添加逗号(,)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!