Limiting entry to numeric characters for a textbox

Posted on 2011-10-31
Last Modified: 2012-05-12
I want to limit entry into a text box to numeric characters (
Question by:Anthony Matovu
    LVL 5

    Expert Comment

    1) Declare a variable to hold JavaScript for enabling only Numbers as follows:
    Dim strNumberOnlyValidation As String = "if (event.keyCode < 48 || event.keyCode > 57) event.returnValue = false;"

    Open in new window

    2) Link the above JavaScript Variable to the requested TextBox as follows in Page_Load:
    MyTextBox.Attributes("onKeypress") = strNumberOnlyValidation

    Open in new window

    LVL 5

    Expert Comment

    You may find a list of JavaScript KeyCodes here:
    LVL 17

    Expert Comment

    Like this

    <asp:TextBox runat="server" id="txtNumber" />
    <asp:RegularExpressionValidator runat="server" id="rexNumber" controltovalidate="txtNumber" validationexpression="^[0-9]+$" errormessage="Please enter digit only!" />


    <asp:TextBox runat="server" id="txtNumber" />
    <asp:RegularExpressionValidator runat="server" id="rexNumber" controltovalidate="txtNumber" validationexpression="\d+" errormessage="Please enter digit only!" />
    LVL 14

    Expert Comment

    Try This:
    <asp:RegularExpressionValidator runat="server" id="rexNumber"
                controltovalidate="txtSN1" validationexpression="(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)" errormessage="Please enter digit only!" />
    LVL 1

    Author Comment

    by:Anthony Matovu
    Sorry, it is a windows application, not an application
    thank you
    LVL 17

    Accepted Solution

    Try this

    Private Function TrapKey(ByVal KCode As String) As Boolean
       If (KCode >= 48 And KCode <= 57) Or KCode = 8 Then
           TrapKey = False
           TrapKey = True
        End If
    End Function

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
            e.Handled = TrapKey(Asc(e.KeyChar))
    End Sub

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    728 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

    18 Experts available now in Live!

    Get 1:1 Help Now