简体   繁体   English

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

[英]Check first character in a string - telephone number

Could you please help me with the validation of a phone number? 您能帮我验证一下电话号码吗? I have already validated it to have 11 characters, but I don't know how to make a condition for the first character to be 0. 我已经验证了它具有11个字符,但是我不知道如何使第一个字符为0的条件。

Thank you! 谢谢!

Here is the code: 这是代码:

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

You can try to validate it this way : 您可以尝试通过以下方式进行验证:

.......
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
.......

That will make sure that tel_no(n) has length exactly 11, and has character in index 0 (the first character) equals zero ( 0 ). 这将确保tel_no(n)长度正好为11,并且索引0中的字符(第一个字符)等于零( 0 )。

Try this: 尝试这个:

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

I should mention though that your user would have a much better UI experience if you showed him a MaskedTextBox instead. 我应该提一下,但是如果您向用户显示MaskedTextBox会给用户带来更好的UI体验。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM