disable alpha caracters in ASP.NET textbox

Posted on 2009-02-17
Last Modified: 2012-05-06
is there any way i can prevent from any alpha car from being entered
Question by:Chris Jones
    LVL 15

    Accepted Solution

    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

    LVL 1

    Author Comment

    by:Chris Jones
    it tells me onkeypress is not a valid attrabute?
    LVL 27

    Expert Comment

    LVL 15

    Expert Comment

    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)")
    LVL 1

    Author Comment

    by:Chris Jones
    ok this works thans

    LVL 1

    Author Closing Comment

    by:Chris Jones
    Thanks for the fast reply

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    This video discusses moving either the default database or any database to a new volume.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now