ASP.NET TextBox filter

2020-05-05 05:29发布

问题:

is there a simple way to suppress certain keystrokes within a textbox? for example if i only want to allow numbers.

回答1:

There's nothing built-in, you will need to write some JavaScript to capture and ignore the keys you want to disallow.

Or you can use this FilteredTextBox control extender, from the ASP .NET AJAX Control Toolkit



回答2:

You could also use a <asp:CompareValidator>. E.g.

<asp:CompareValidator ID="valNumbersOnly" runat="server"
    ControlToValidate="controlYouWantToValidate"
    Operator="DataTypeCheck"
    Type="Integer"
    ErrorMessage="Please enter only numbers">*</asp:CompareValidator>

Or you could go even further and use a regular expression validator.

These solutions will work with and without javascript, so they will validate client side and server side. Not everyone has javascript turn on!

HTHs, Charles



标签: asp.net