954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to limit a textbox to only accept numbers, backspace and a certain length?

Hey

I want to control a textbox to only allow numbers, backspace and a certain length. Ive tried with the IsNumeric function but I cant seem to get it to work.

Thanks.

riahc3
Posting Pro
545 posts since May 2008
Reputation Points: 50
Solved Threads: 2
 
sandeepparekh9
Posting Whiz
367 posts since Dec 2010
Reputation Points: 123
Solved Threads: 71
 

Hi,

To create a numeric textbox, look here.

To select a certain length, go to the properties of that textbox and set the Maxlength.

Luc001
Posting Whiz
376 posts since Mar 2010
Reputation Points: 85
Solved Threads: 98
 

Just put this code in your keypress event in your textbox

Select Case e.KeyChar
            Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", vbBack
                e.Handled = False
            Case Else
                e.Handled = True
        End Select


if you want to change the input length of textbox just click the properties and then change the MaxLength Value.

ryan311
Posting Whiz in Training
254 posts since Jul 2008
Reputation Points: 3
Solved Threads: 5
 

Just put this code in your keypress event in your textbox

Select Case e.KeyChar
            Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", vbBack
                e.Handled = False
            Case Else
                e.Handled = True
        End Select

if you want to change the input length of textbox just click the properties and then change the MaxLength Value.



Nice solution!
Thanks so much! :)

skran
Light Poster
43 posts since Nov 2011
Reputation Points: 19
Solved Threads: 2
 
If Textbox1.Text = "" Then
            GoTo ex

        Else
            If IsNumeric(Textbox1.Text) Then
                GoTo ex
            Else
                MsgBox("You cannot add alpha characters, please input the Integer or numbers !", vbCritical)
                Textbox1.Text = ""
                searchback()
            End If
        End If
ex:
        Exit Sub
princenathan
Newbie Poster
8 posts since Oct 2010
Reputation Points: 8
Solved Threads: 2
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You