user2308593
user2308593

Reputation: 29

Enter numeric value only into textbox in visual basic

I am trying to set up a form to accept a telephone number, but i am unsure of how to validate it so it will only take numeric values with 11 digits.

So far i have it working to ensure that there is something in the textbox

 'Validate data for Telephone Number
 If txtTelephoneNumber.Text = "" Then
 txtTelephoneNumber.Focus()
 MessageBox.Show("You must enter a Telephone Number.", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Information)

Upvotes: 0

Views: 47939

Answers (7)

Fullyworked
Fullyworked

Reputation: 13

In your textbox keydown event. Use the following:

If Not IsNumeric(Chr(e.KeyCode)) Then
    e.SuppressKeyPress = True
End If

If you want allow other characters you would do it like so:

If Not IsNumeric(Chr(e.KeyCode)) And Not e.KeyCode = 8 And Not e.KeyCode = 46 Then
    e.SuppressKeyPress = True
End If

'(8 = backspace key and 46 = Delete key)

Upvotes: 1

user3700224
user3700224

Reputation: 1

You can use the lostfocus event and in this event you can put something like this:

if not isnumeric(textbox1.text) then
   textbox1.gotfocus   'this part is to validate only numbers in the texbox and not let
                        ' him go from the texbox
endif

and you should define the maxlength for the textbox in just 11 characters

Upvotes: 0

Jordan B
Jordan B

Reputation: 1

set max length to 12 put this in keypress of text1 and it will format it with dashes and only numbers

    If Len(Text1.Text) = 3 Or Len(Text1.Text) = 7 Then
        Text1.Text = Text1.Text & "-"
        Text1.SelStart = Len(Text1.Text)
    End If
    If KeyAscii <> 127 And KeyAscii <> 8 And KeyAscii <> 13 Then
        If IsNumeric(Chr(KeyAscii)) = False Then
            KeyAscii = 0
        End If
    ElseIf KeyAscii = 8 Then
        If Right(Text1.Text, 1) = "-" Then
            Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1)
            Text1.SelStart = Len(Text1.Text)
        End If
    End If

if you only want numeric text box use the following in the keypress

    If KeyAscii <> 127 And KeyAscii <> 8 And KeyAscii <> 13 Then
        If IsNumeric(Chr(KeyAscii)) = False Then
            KeyAscii = 0
        End If
    End If

Upvotes: 0

Michael Parr
Michael Parr

Reputation: 134

I use this for the keypress event

If e.KeyChar < CStr(0) Or e.KeyChar > CStr(9) Then e.Handled = True

Actually that looks different to what i remember using, but it works. Though you'll need to allow backspace too.

Or i guess even shorter would be

If Not IsNumeric(e.KeyChar) Then e.Handled = True

also keypress event.

You can set the maximum length of a textbox using MaxLength

Upvotes: 0

APrough
APrough

Reputation: 2701

Put this in your KeyPress event of the TextBox

    'makes sure that only numbers and the backspace are allowed in the fields.
    Dim allowedChars As String = "0123456789" & vbBack

    'get a reference to the text box that fired this event
    Dim tText As TextBox
    tText = CType(sender, TextBox)

    If allowedChars.IndexOf(e.KeyChar) = -1 Then
        ' Invalid Character
        e.Handled = True
    End If

    if tText.Text.Length >= 11 then
        e.Handled = True
    End If

Upvotes: 1

phadaphunk
phadaphunk

Reputation: 13313

I'll imply you are using Windows Forms.
Write this as your TextBox's Key Pressed event.

Private Sub myTxtBox_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles myTxtBox.KeyPress
If txtTelephoneNumber.Text.Length > 11 Then
   e.Handled= True
   return
End If
If Asc(e.KeyChar) <> 8 Then
    If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
          e.Handled = True
    End If
End If
End Sub

This should (did not have time to test it) keep the user from inputing anything that is not a number. It should prevent him from inputting more than 11 numbers too.

Upvotes: 3

tymeJV
tymeJV

Reputation: 104775

You can try

If txtTelephoneNumber.Text = "" Or Not IsNumeric(txtTelephoneNumber.Text) Or txtTelephoneNumber.Text.Length <> 11 Then
    txtTelephoneNumber.Focus()
    MessageBox.Show("You must enter a Telephone Number.", "Data Entry Error",      MessageBoxButtons.OK, MessageBoxIcon.Information)

Upvotes: 0

Related Questions