验证文本框 - 仅允许编号和期间。

问题描述:

我希望我的文本框仅接受数字,但是当用户输入12.50时它仍然可以接受该值。



所以我的问题是我该如何允许我的文本框只接受数字和句点?

I want my textbox to accept numbers only, however when a user inputs "12.50" It can still accept the value.

SO my question is how do I allow my textbox to accept number and period only?

使用 RegularExpressionValidator [ ^ ]



使用此正则表达式 [ ^ ]


谷歌是你的朋友: Google [ ^ ]

请注意,您不应该在C#中执行此操作 - JavaScript这里很好 - 因为它需要每个按键进行到服务器的往返,这会使用户输入减慢到愚蠢的水平。
google is your friend here: Google[^]
Note that you should not do this in C# - JavaScript is good here - as it would require each keypress taking a round trip to the server, and that would slow user input to silly levels.


只需使用ajaxcontroltoolkit你可以做一个FilterTextboxExtender选项它很容易使用下面的代码

Ajax代码

Just use ajaxcontroltoolkit there is a FilterTextboxExtender option you can do it easily using following code
Ajax code
<ajaxtoolkit:toolkitscriptmanager runat="server" xmlns:ajaxtoolkit="#unknown"></ajaxtoolkit:toolkitscriptmanager>
        <asp:textbox id="TextBox1" runat="server" xmlns:asp="#unknown"></asp:textbox>
        <ajaxtoolkit:filteredtextboxextender id="fte" runat="server" targetcontrolid="TextBox1" xmlns:ajaxtoolkit="#unknown">
            FilterMode="ValidChars" FilterType="Custom" ValidChars="012345689."></ajaxtoolkit:filteredtextboxextender>