检查字符串中的第一个字符 - 电话号码

时间:2014-03-15 19:39:55

标签: .net vb.net string

你能帮我验证电话号码吗?我已经验证它有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

2 个答案:

答案 0 :(得分: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)。

答案 1 :(得分:0)

试试这个:

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,那么你的用户会有更好的用户体验。

相关问题