We help IT Professionals succeed at work.

Only allow numbers to be entered in text box...

1,316 Views
Last Modified: 2012-06-22
Hello all, I have a text box that I only want the user to be able to enter number type entries. I want them to be able to enter 1, -1, or 1.1 but not one. I have this right now but it doesn't allow me to enter a decimal or a neg sign.....Can anyone tell me how I can use this code or some other keypress code to limit the user imput?

Protected Sub textbox_OnKeyPress(ByVal Sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles textbox.KeyPress

        'Validate that the only thing the user can enter is a number
        If [Char].IsNumber(e.KeyChar) OrElse AscW(e.KeyChar) = CInt(Keys.Back) Then
            e.Handled = False
            Return
        End If
        e.Handled = True
    End Sub
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
"Why not use the on exit event, using IsNumeric?   Much simpler..."

Would this stop someone from being able to enter alpha or special characters? (ABC ?><)

"Have you tried adding an extra test to your if statement and adding the specific "-" and "." to the list. That should solve your problem."
How would I do this?
And as metthewspatric suggested you will need to do some checking in the on exit event to make sure for example that the user didn't enter "----1" or .....1, etc
CERTIFIED EXPERT
Top Expert 2010

Commented:
Boon01 said:
>>"Why not use the on exit event, using IsNumeric?   Much simpler..."
>>
>>Would this stop someone from being able to enter alpha or special characters? (ABC ?><)

People would be able to enter such characters, but then the code would force people to go
back and change it.
CERTIFIED EXPERT
Top Expert 2011
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
I made a simple change to your if statement:

      If [Char].IsNumber(e.KeyChar) OrElse AscW(e.KeyChar) = CInt(Keys.Back) OrElse AscW(e.KeyChar) = AscW("-") OrElse AscW(e.KeyChar) = AscW(".") Then
Chris_bottomley has it best. Simple and easy to read.
hhmm, I tried the examle Chris gave and it does not compile under VB.NET 2003, Chris do you know the syntax for VB.NET?
Here it is modified for VB.NET

      If Not Chr(AscW(e.KeyChar)) Like "[0-9.-]" Then
         e.Handled = True
      Else
         e.Handled = False
      End If

Author

Commented:
matthewspatrick, do you happen to have an example of what how to validate on exit that you suggested?

thanks Anthony2000 your code mod worked great. I just have to do what mathewspatrick suggested too because now the code allows (...) or (---) which give a big fat error when saving to the database.

Author

Commented:
Alright, this is what I have so far....(I took out the Neg sign-turns out I didn't need it)

Protected Sub textbox_OnKeyPress(ByVal Sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles textbox.KeyPress

        'Validate that the only thing the user can enter is a number
        If (Char.IsDigit(e.KeyChar) _
        Or e.KeyChar = "." And InStr(Me.textbox.Text, ".") = False _
        Or e.KeyChar = Chr(Keys.Back)) Then
            e.Handled = False
            Return
        End If
        e.Handled = True
    End Sub

I think this will work, unless you guys see something that may cause me issues.
I tried it, seems to work great.

Author

Commented:
Thanks for all the help!!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

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

OR

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.