0

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)
1
  • 2
    The MaskedTextBox control was made for this. Commented Apr 22, 2013 at 18:52

7 Answers 7

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Need to do If txtTelephoneNumber.Text.Length > 11. Otherwise, first KeyPress is handled.
1

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

Comments

1

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)

Comments

0

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)

Comments

0

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

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.