We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now


disable alpha caracters in ASP.NET textbox

Chris Jones
Chris Jones asked
Medium Priority
Last Modified: 2012-05-06
is there any way i can prevent from any alpha car from being entered
Watch Question

You would use javascript.  This will only allow numbers to be entered.

If you want, you can add the attribute in the form_load function

textbox1.attributes.add("onkeypress","return isNumberKey(event)")
<SCRIPT language=Javascript>
      function isNumberKey(evt)
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;
         return true;
<asp:TextBox id="textbox1" runat="server" onkeypress="return isNumberKey(event)" />

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Chris JonesLead Application Web Developer


it tells me onkeypress is not a valid attrabute?
Top Expert 2009

It's not, but it should still work... if not you can add it during runtime in the Page_Load

<asp:TextBox id="textbox1" runat="server"  />

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    textbox1.Attributes.Add("onkeypress","return isNumberKey(event)")
Chris JonesLead Application Web Developer


ok this works thans

Chris JonesLead Application Web Developer


Thanks for the fast reply
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.