Reputation: 13
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.
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
Upvotes: 1
Views: 94
Reputation: 89315
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
).
Upvotes: 1
Reputation: 10478
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.
Upvotes: 0