r/visualbasic • • Nov 02 '20

Formatting Question Help

Post image
6 Upvotes

9 comments sorted by

View all comments

Show parent comments

3

u/snang Moderator Nov 02 '20 edited Nov 02 '20

So the first one is easy. You just check the Length of the Text property for the phone number control.

If TextPhone.Text.Length < 11 then MsgBox("Bad Phone Number")

The second, without using RegEx, you're gonna have to loop through the string checking each character and making sure it's a letter, number, or space.

You can accomplish this using a For Next loop and the Substring function on the Text property for the postal code control.

For x = 0 to TextPostal.Text.Length - 1
    If TextPostal.Text.Substring(x,1) = "Whatever"
Next

Also, to clarify, if you're not supposed to use stuff like For Next you can just check each character individually using the same Substring logic.

TextPostal.Text.Substring(1,1)
TextPostal.Text.Substring(2,1)
TextPostal.Text.Substring(3,1)
...etc

MSDN: For Next Statement, Substring

1

u/FudgeDonut Nov 02 '20

1

u/PostalElf VB.Net Intermediate Nov 03 '20
Sub Main()
    Dim letters As String = Console.ReadLine()
    If CheckLetters(letters) = False Then
        Console.WriteLine("Letters only please.")
    Else
        Console.WriteLine("You've entered letters only. Hooray.")
    End If
    Console.ReadLine()
End Sub

Public Function CheckLetters(ByVal letters As String) As Boolean
    Dim alphabet As Char() = "abcdefghijklmnopqrstuvwxyz"
    For Each c In letters
        If alphabet.Contains(c) = False Then
            Return False
        End If
    Next
    Return True
End Function

1

u/FudgeDonut Nov 03 '20

Thank you!