如何将输入框限制为 vb 中的 11 个数值
how can i limit inputbox to 11 numerical values in vb
如何在 vb 中将输入框限制为 11 个数值?我想让一个人能够编辑 phone 号码,但我当前的代码似乎有问题。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each x As ListViewItem In lvCustomers.Items
Dim contactEdit As String
If x.Checked Then
contactEdit = CInt(InputBox("Modify contact no."))
Do Until contactEdit.Count = 11
MessageBox.Show("Maximum numerical digits of 11")
Loop
x.SubItems(5).Text = contactEdit
x.Checked = False
End If
Next
End Sub
您可以使用正则表达式来检查:
Imports System.Text.RegularExpressions
'[...]
Dim isValid As Boolean = Regex.Match("12345", "^\d{0,11}$").Success()
您的代码可以如下所示:
Imports System.Text.RegularExpressions
'[...]
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each x As ListViewItem In lvCustomers.Items
Dim contactEdit As String
If x.Checked Then
contactEdit = InputBox("Modify contact no.")
Do Until Regex.Match(contactEdit, "^\d{0,11}$").Success()
MessageBox.Show("Maximum numerical digits of 11")
contactEdit = InputBox("Modify contact no.")
Loop
x.SubItems(5).Text = contactEdit
x.Checked = False
End If
Next
End Sub
您可以在 RegExp ({minValue,maxValue}
) 上定义输入的最小和最大长度。输入必须是数字 (\d
)。您可以在此站点上找到所用 RegExp 的说明:https://regex101.com/r/6h7z2u/1
提示: 我建议从 InputBox
中删除 CInt
,因为如果 [=15] 中的值,代码会抛出 Exception
=] 不是有效的整数。使用该解决方案的正则表达式,只能将数字写入 InputBox
.
您可以使用 Len(contactEdit.Text) = 11 而不是 contactEdit.Count = 11
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each x As ListViewItem In lvCustomers.Items
Dim contactEdit As String
If x.Checked Then
contactEdit = CInt(InputBox("Modify contact no."))
Do Until Len(contactEdit.Text) >= 11
MessageBox.Show("Maximum numerical digits of 11")
Loop
x.SubItems(5).Text = contactEdit
x.Checked = False
End If
Next
End Sub
如何在 vb 中将输入框限制为 11 个数值?我想让一个人能够编辑 phone 号码,但我当前的代码似乎有问题。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each x As ListViewItem In lvCustomers.Items
Dim contactEdit As String
If x.Checked Then
contactEdit = CInt(InputBox("Modify contact no."))
Do Until contactEdit.Count = 11
MessageBox.Show("Maximum numerical digits of 11")
Loop
x.SubItems(5).Text = contactEdit
x.Checked = False
End If
Next
End Sub
您可以使用正则表达式来检查:
Imports System.Text.RegularExpressions
'[...]
Dim isValid As Boolean = Regex.Match("12345", "^\d{0,11}$").Success()
您的代码可以如下所示:
Imports System.Text.RegularExpressions
'[...]
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each x As ListViewItem In lvCustomers.Items
Dim contactEdit As String
If x.Checked Then
contactEdit = InputBox("Modify contact no.")
Do Until Regex.Match(contactEdit, "^\d{0,11}$").Success()
MessageBox.Show("Maximum numerical digits of 11")
contactEdit = InputBox("Modify contact no.")
Loop
x.SubItems(5).Text = contactEdit
x.Checked = False
End If
Next
End Sub
您可以在 RegExp ({minValue,maxValue}
) 上定义输入的最小和最大长度。输入必须是数字 (\d
)。您可以在此站点上找到所用 RegExp 的说明:https://regex101.com/r/6h7z2u/1
提示: 我建议从 InputBox
中删除 CInt
,因为如果 [=15] 中的值,代码会抛出 Exception
=] 不是有效的整数。使用该解决方案的正则表达式,只能将数字写入 InputBox
.
您可以使用 Len(contactEdit.Text) = 11 而不是 contactEdit.Count = 11
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each x As ListViewItem In lvCustomers.Items
Dim contactEdit As String
If x.Checked Then
contactEdit = CInt(InputBox("Modify contact no."))
Do Until Len(contactEdit.Text) >= 11
MessageBox.Show("Maximum numerical digits of 11")
Loop
x.SubItems(5).Text = contactEdit
x.Checked = False
End If
Next
End Sub