2

I have a textbox and I need to let user enter only amount of money, which means only numbers and a comma

here is what I have

 <asp:TextBox ID="txt_tutar" runat="server" onkeypress="this.value=this.value.replace(/\D/g,'')"></asp:TextBox>

this code above only allows numeric, it deletes the comma. how can I update to let user enter float number in textbox ?

4

2 回答 2

5

使用这个正则表达式:

/[^\d,]/g

如:

this.value=this.value.replace(/[^\d,]/g,'')

当然,这并不意味着字符串将是一个有效的数值;eg2,,0,1会被认为是好的。你真的应该使用decimal.TryParse或类似的东西进行完整的服务器端验证

于 2013-06-26T19:09:05.903 回答
0

在这里,您还可以使用必填字段验证器来执行此操作,

更好的方法是在客户端使用 javascript 来提高性能,使用下面的脚本

function isValidNo(str) {
            var inbadChars = "\t\n\r^&*=~`';<>?[]{}abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
            //debugger;
            for (n = 0; n < str.length; n++) //filter for invalid chars
            {
                if (inbadChars.indexOf(str.charAt(n)) != -1) return false;
            }
            return true;
        }

建议也使用服务器端 tryparse,因为在某些浏览器上有时脚本可能不起作用。

于 2013-06-28T10:48:48.880 回答