0

我正在尝试设置一个表格来接受电话号码,但我不确定如何验证它,所以它只会采用 11 位数字的数值。

到目前为止,我一直在努力确保文本框中有内容

 '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)
4

7 回答 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.

于 2013-04-22T18:49:40.583 回答
1

在你的textbox keydown事件中。使用以下内容:

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

如果您想允许其他字符,您可以这样做:

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)
于 2017-03-29T05:02:05.457 回答
0

你可以使用 lostfocus 事件,在这种情况下你可以这样写:

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

并且您应该仅用 11 个字符定义文本框的最大长度

于 2014-06-02T16:52:35.263 回答
0

把它放在 TextBox 的 KeyPress 事件中

    '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
于 2013-04-22T20:16:04.230 回答
0

你可以试试

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)
于 2013-04-22T18:51:26.007 回答
0

我将它用于按键事件

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

实际上,这看起来与我记得使用的不同,但它有效。尽管您也需要允许退格。

或者我想更短的会是

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

还有按键事件。

您可以使用 MaxLength 设置文本框的最大长度

于 2013-04-23T05:24:46.393 回答
0

将最大长度设置为 12 将其放在 text1 的按键中,它将使用破折号和仅数字来格式化

    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 KeyAscii <> 127 And KeyAscii <> 8 And KeyAscii <> 13 Then
        If IsNumeric(Chr(KeyAscii)) = False Then
            KeyAscii = 0
        End If
    End If
于 2014-03-05T21:51:34.217 回答