Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm writing a password checker program in visual basic 2010, and it keeps saying whenever letters are in the password 'Conversion from string "Helping5" to type 'Boolean' is not valid.' (with 'Helping5' being a random password that should be strong.
Please help! I'm new to this coding and would appreciate easy to understand instructions.
My code:


VB
Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        If TextBox1.Text = ValidatePassword(True) Then
            Form2.Show()

        Else
            Form4.Show()
        End If

        If TextBox1.Text = ValidatePassword(False) Then
            Form3.Show()

        Else
            Form4.Show()
        End If



    End Sub

    Private Sub CheckBox1_CheckedChanged(sender As System.Object, e As System.EventArgs)

    End Sub

    Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged


    End Sub
    Function ValidatePassword(ByVal pwd As String,
    Optional ByVal minLength As Integer = 6,
    Optional ByVal maxLength As Integer = 12,
    Optional ByVal numUpper As Integer = 1,
    Optional ByVal numLower As Integer = 1,
    Optional ByVal numNumbers As Integer = 1,
    Optional ByVal numSpecial As Integer = 1) As Boolean

        ' Replace [A-Z] with \p{Lu}, to allow for Unicode uppercase letters. 
        Dim upper As New System.Text.RegularExpressions.Regex("[A-Z]")
        Dim lower As New System.Text.RegularExpressions.Regex("[a-z]")
        Dim number As New System.Text.RegularExpressions.Regex("[0-9]")
        ' Special is "none of the above". 
        Dim special As New System.Text.RegularExpressions.Regex("[^a-zA-Z0-9]")

        ' Check the length. 
        If Len(pwd) < minLength Then Return False
        ' Check for minimum number of occurrences. 
        If upper.Matches(pwd).Count < numUpper Then Return False
        If lower.Matches(pwd).Count < numLower Then Return False
        If number.Matches(pwd).Count < numNumbers Then Return False

        ' Passed all checks. 
        Return True
    End Function

    Private Sub TextBox1_TextChanged()
        Throw New NotImplementedException
    End Sub

    Private Function ValidatePassword() As String
        Throw New NotImplementedException
    End Function



End Class
Posted
Updated 27-Apr-14 8:50am
v2

you need to understand what is method parameters and return type. ValidatePassword need password as parameter but you sending true or false values. that is incorrect and if condition should check the return value of the ValidatePassword as below

VB
If ValidatePassword(TextBox1.Text) Then
Form2.Show()
Else
Form4.Show()
End If
 
Share this answer
 
v2
Damith Weerasinghe has a good point but I'm going to a little further.

I created a test version to test your function.

First you are not testing if the textbox is empty or not. if it is empty then you may end up with a null reference exception when the button is clicked.

You are using two seperate If statements.

VB
Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
    Dim pwd As String
    If tbInput.Text = Nothing Then
        MsgBox("No text entered")
        Exit Sub
    Else
        pwd = tbInput.Text
    End If

    If ValidatePassword(pwd) = True Then
        Form2.Show()
    ElseIf ValidatePassword(pwd) = False Then
        Form4.Show()
    End If

    'this just output to another texbox true or false
    Dim output As String = ValidatePassword(pwd).ToString
    tbOutput.Text = output

End Sub


Modifying your code to read like:
VB
Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
    Dim pwd As String
    If tbInput.Text = Nothing Then
        MsgBox("No text entered")
        Exit Sub
    Else
        pwd = tbInput.Text
    End If

    If ValidatePassword(pwd) Then ' = true
        Form2.Show()

    Else
        Form4.Show()
    End If

    If ValidatePassword(pwd) Then
        Form3.Show()

    Else
        Form4.Show()
    End If

    'If ValidatePassword(pwd) = True Then
    '    Form2.Show()
    'ElseIf ValidatePassword(pwd) = False Then
    '    Form4.Show()
    'End If

    'this just output to another texbox true or false
    Dim output As String = ValidatePassword(pwd).ToString
    tbOutput.Text = output

End Sub


If it passes it will open forms 2 and 3
if it fails it will open only form 4 once.

passing back a boolean value is ok for a test but it does not tell the user what part they failed in.
Also in a final version you may want to add the ability to change the values instead of using the default ones.
If you are wanting to check the text entered as it is being input you can use the
TextBox1_TextChanged

and just pass the value in the textbox1.text.
you will still need some form of feed back on what is not correct.
you may look into using the error provider.
http://msdn.microsoft.com/en-us/library/vstudio/a0d996e0(v=vs.100).aspx[^]

Or some other form of feedback

Also the string "AAaa11" passes as valid using the default values which would not be a good passsword.

I hope that helps
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900