r/visualbasic • u/AlphaBear718 • Mar 02 '21
VB.NET Help Confused why this Boolean comes back as False
Hey guys, fairly new to VB. I'm trying to write a function that will check a textbox (password) to check if it has at least 8 letters, 2 numbers, and one uppercase letter. So far I have it so it will tell me if there are 8 letters. I'm somewhat confused on why this function returns false once I put any numbers in it though. Any guidance?
Function PasswordCheck(password As String) As Boolean
Dim passwordletter As String
Dim passvalue As Integer = (password.Length) - 1
Dim pass As Integer
If passvalue < 7 Then
Return False
Else
For p As Integer = 0 To passvalue
passwordletter = password.Substring(p, 1)
If IsNumeric(passwordletter) Then
pass += 1
If pass < 2 Then
Return False
ElseIf pass >= 2 Then
Return True
End If
End If
Next
End If
End Function5
u/farquaad Mar 02 '21 edited Mar 02 '21
pass += 1
If pass < 2 Then
Return FalseYour pass value is never counting past 1 if you start checking it's value immediately.
I would suggest:
For
Count numerical values
Loop
If pass < 2 then return false1
1
u/revennest Mar 03 '21
Don't cut corner, if you check 2 subject then you need 2 variant to check it unless you use bitmask, if a password has 3 numbers and an upper letter, you code still give it a pass.
Function check_password(Password As String) As Boolean
If Password.Length < 8 Then Return False
Dim Upper = 0, Number = 0
For Each Item In Password
Select Case True
Case Char.IsNumber(Item)
Number += 1
Case Char.IsUpper(Item)
Upper += 1
End Select
If Number >= 2 And Upper >= 2 Then Return True
Next
Return False
End Function
2
u/laancelot Mar 02 '21
Is the `password` parameter coming from the actual textbox? Let's say the the Textbox is called `PasswordTextbox`, then you would have to send `PasswordTextbox.Text` to your method, and it should work fine.