如何将输入框限制为vb中的11个数值

时间:2017-06-16 13:33:50

标签: vb.net inputbox

如何在vb中将输入框限制为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 contactEdit.Count = 11
                MessageBox.Show("Maximum numerical digits of 11")
            Loop

            x.SubItems(5).Text = contactEdit

            x.Checked = False

        End If
    Next
End Sub

2 个答案:

答案 0 :(得分:1)

您可以使用Regex来检查:

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

提示:我建议从CInt移除InputBox,因为如果Exception的值不是InputBox,代码会抛出InputBox一个有效的整数。使用解决方案的RegExp,只能将数字写入import React from 'react'; import { connect } from 'react-redux'; import { showDropdown } from '../../redux/actions/dropdownActions'; function StreamWidget(props) { const onClickHandler = () => props.showDropdown() return ( <div> <a href='#' role='button' onClick={ onClickHandler } /> </div> ); } export default connect(null, { showDropdown })(StreamWidget);

答案 1 :(得分:0)

您可以使用 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