ASP.NET TextBox filter

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

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 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

Solution 2

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

<asp:CompareValidator ID="valNumbersOnly" runat="server"
    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!


Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply