Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Limiting entry to numeric characters for a textbox

Posted on 2011-10-31
6
Medium Priority
?
411 Views
Last Modified: 2012-05-12
I want to limit entry into a text box to numeric characters (vb.net)
0
Comment
Question by:Anthony Matovu
6 Comments
 
LVL 5

Expert Comment

by:bitref
ID: 37060845
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

0
 
LVL 5

Expert Comment

by:bitref
ID: 37060849
You may find a list of JavaScript KeyCodes here:
0
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 37060876
Like this

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

OR

<asp:TextBox runat="server" id="txtNumber" />
<asp:RegularExpressionValidator runat="server" id="rexNumber" controltovalidate="txtNumber" validationexpression="\d+" errormessage="Please enter digit only!" />
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 14

Expert Comment

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

http://regexlib.com/DisplayPatterns.aspx?cattabindex=2&categoryId=3
0
 
LVL 1

Author Comment

by:Anthony Matovu
ID: 37061041
Sorry, it is a windows application, not an asp.net application
thank you
0
 
LVL 17

Accepted Solution

by:
sonawanekiran earned 2000 total points
ID: 37061333
Try this

Private Function TrapKey(ByVal KCode As String) As Boolean
   If (KCode >= 48 And KCode <= 57) Or KCode = 8 Then
       TrapKey = False
   Else
       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
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
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 …
Integration Management Part 2
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month13 days, 16 hours left to enroll

580 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