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

Windows フォームを使用していることを暗示します。
これを TextBox の Key Pressed イベントとして記述します。

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

これにより(テストする時間がありませんでした)、ユーザーが数値以外を入力するのを防ぐことができます。11 個を超える数字も入力できないようにする必要があります。

于 2013-04-22T18:49:40.583 に答える
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 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 に答える