最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - textbox only allow numeric and comma - Stack Overflow

programmeradmin4浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论