【问题标题】:Check first character in a string - telephone number检查字符串中的第一个字符 - 电话号码
【发布时间】:2014-03-15 19:39:55
【问题描述】:

您能帮我验证电话号码吗?我已经验证它有 11 个字符,但我不知道如何使第一个字符为 0 的条件。

谢谢!

代码如下:

Do
        tel_no(n) = InputBox("Enter the telephone number")
        If Len(tel_no(n)) < 11 Or Len(tel_no(n)) > 11 Then
            MsgBox("The telephone number should have 11  digits and should start with 0")
        End If

Loop Until Len(tel_no(n)) = 11

【问题讨论】:

    标签: .net vb.net string


    【解决方案1】:

    您可以尝试以这种方式验证它:

    .......
    tel_no(n) = InputBox("Enter the telephone number")
    If (tel_no(n).Length <> 11) Or (tel_no(n)(0) <> "0") Then
        MsgBox("The telephone number should have 11  digits and should start with 0")
    End If
    .......
    

    这将确保 tel_no(n) 的长度正好为 11,并且索引 0 中的字符(第一个字符)等于零 (0)。

    【讨论】:

    • 正是我需要的。谢谢@har07。祝你有个美好的一天。
    【解决方案2】:

    试试这个:

    Dim conditionMet As Boolean = False
    
    Do
        Dim phoneNumber As String = InputBox("Enter the telephone number")
    
        conditionMet = phoneNumber.Length = 11 And phoneNumber.StartsWith("0")
    
        If Not conditionMet Then
            MsgBox("The telephone number should have 11 digits and should start with 0")
        Else
            tel_no(n) = phoneNumber
        End If
    
    Loop Until conditionMet
    

    我应该提一下,如果您向他展示 MaskedTextBox,您的用户将获得更好的 UI 体验。

    【讨论】:

    • 我用过上面那个,因为它更短。无论如何,这似乎也不错。欢呼帮助。
    猜你喜欢
    • 2017-10-17
    • 1970-01-01
    • 1970-01-01
    • 2012-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-01
    相关资源
    最近更新 更多