r/visualbasic • • Nov 02 '20

Formatting Question Help

Post image
6 Upvotes

9 comments sorted by

1

u/FudgeDonut Nov 02 '20 edited Nov 03 '20

Can anyone send me an example of what this would look like? Spent two nights on this and I swear we haven't covered it. If I can ask too, that you basically ELI5 because I started VB in college (uk) a month ago, after no previous programming education. [Visual Basic .NET Core]

EDIT: ***SOLVED***

1

u/snang Moderator Nov 02 '20

Are you allowed to use RegEx?

1

u/FudgeDonut Nov 02 '20

No, sorry

1

u/FudgeDonut Nov 02 '20

I'm at a very basic stage right now, we've been mostly taught Loop and some numeric formatting

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!

1

u/CluelessBicycle Nov 04 '20 edited Nov 04 '20

Use libphonenumber from google, which is how the company that I work for does it; we sent 80 million SMS messages last month, so we have to validate phone numbers quite frequently.

https://blog.appharbor.com/2012/02/03/net-phone-number-validation-with-google-libphonenumber