javascript - textbox only allow numeric and comma - Stack Overflow

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

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

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 ma. how can I update to let user enter float number in textbox ?

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

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 ma. how can I update to let user enter float number in textbox ?

Share Improve this question edited Jun 26, 2013 at 19:09 Ian 51k13 gold badges104 silver badges111 bronze badges asked Jun 26, 2013 at 19:07 Arif YILMAZArif YILMAZ 5,88629 gold badges114 silver badges197 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 5

Use this regular expression:

/[^\d,]/g

As in:

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

Of course this doesn't really mean the string will be a valid numeric value; e.g. 2,,0,1 would be considered fine. You really should be doing full server-side validation using decimal.TryParse or something similar

Here you can also use required field validator to do ths,

Better way go with javascript at client side for performance, use below script

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;
        }

Reended to use server side tryparse as well, as on somebrowser sometimes scripets may not work.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744344591a4569604.html

相关推荐

  • javascript - textbox only allow numeric and comma - Stack Overflow

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

    8天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信