密码检查器visual basic 2010帮助? [英] Password checker visual basic 2010 help?

查看:61
本文介绍了密码检查器visual basic 2010帮助?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在visual basic 2010中编写了一个密码检查程序,并且每当密码中出现字母时,它都会一直说从字符串转换Helping5到'Boolean'无效。'(带'Helping5' 是一个随机密码,应该是强大的。

请帮助!我是这个编码的新手,并希望通过易于理解的说明。

我的代码:




 公开  Form1 

私有 Sub Form1_Load(sender As System。 Object ,e As System.EventArgs) 句柄 MyBase .Load

结束 Sub

P rivate Sub Button1_Click(发件人作为系统。对象,e As System.EventArgs)句柄 Button1.Click
如果 TextBox1.Text = ValidatePassword( True 那么
Form2.Show()

Else
Form4.Show()
结束 如果

如果 TextBox1。 Text = ValidatePassword( False 然后
Form3.Show()

Else
Form4.Show()
结束 如果



结束 Sub

私有 Sub CheckBox1_CheckedChanged(sender As System。 Object ,e As System.EventArgs)

结束 Sub

专用 Sub TextBox1_TextChanged(发件人作为系统。 Object ,e As System.EventArgs)句柄 TextBox1.TextChanged


结束 Sub
功能 ValidatePassword( ByVal pwd 作为 字符串
可选 ByVal minLength 作为 整数 = 6
可选 ByVal maxLength 作为 整数 = 12
可选 ByVal numUpper As 整数 = 1
可选 ByVal numLower 作为 整数 = 1
可选 ByVal numNumbers 作为 整数 = 1
可选 ByVal numSpecial As 整数 = 1 As Boolean

' 将[AZ]替换为\p {Lu},以允许使用Unicode大写字母。
Dim upper As New System.Text.RegularExpressions.Regex( [AZ]
Dim lower As System.Text。 RegularExpressions.Regex( [az]
Dim number As New System.Text.RegularExpressions.Regex( [0-9]
' 特殊是以上都不是。
Dim special As New System.Text.RegularExpressions.Regex( [^ a-zA-Z0-9]

' 检查长度。
如果 Len(pwd)< minLength 然后 返回 错误
' 检查最小出现次数。
如果 upper.Matches(pwd).Count< numUpper 然后 返回 错误
如果 lower.Matches(pwd).Count< numLower 然后 返回 错误
如果 number.Matches(pwd).Count< numNumbers 然后 返回 错误

' 通过所有检查。
返回 True
结束 功能

私有 Sub TextBox1_TextChanged()
抛出 NotImplementedException
结束 Sub

私有 功能 ValidatePassword()作为 String
抛出 NotImplementedException
结束 功能



结束 C. lass

解决方案

您需要了解什么是方法参数和返回类型。 ValidatePassword需要密码作为参数,但您发送true或false值。这是不正确的,如果条件应该检查​​ValidatePassword的返回值如下



 如果 ValidatePassword(TextBox1.Text)那么 
Form2.Show()
Else
Form4.Show()
结束 如果


Damith Weerasinghe有一个很好的观点,但我会更进一步。



我创建了一个测试你的功能测试版。



首先你不测试文本框是否为空。如果它是空的,那么当你点击按钮时你可能会得到一个空的引用异常。



你正在使用两个单独的If语句。



 私人  Sub  btnTest_Click(  ByVal  sender  As  System。 Object ,< span class =code-keyword> ByVal  e  As  System.EventArgs)句柄 btnTest .Click 
Dim pwd As String
如果 tbInput.Text = Nothing 然后
MsgBox( 没有输入文字
< span class =code-keyword>退出 Sub
Else
pwd = tbInput.Text
< span class =code-keyword>结束 如果

如果 ValidatePassword(pwd)= True 然后
Form2.Show()
ElseIf ValidatePassword(pwd)= False 然后
Form4.Show()
结束 如果

' 这只输出到另一个texbox true或false
Dim output As String = ValidatePassword(pwd).ToString
tbOutput.Text =输出

结束 Sub





修改您的代码,如下所示:

 私有  Sub  btnTest_Click( ByVal  sender  As  System。 Object  ByVal  e  As  System.EventArgs)句柄 btnTest.Click 
Dim pwd As String
如果 tbInput.Text = Nothing 然后
MsgBox( 没有输入文字
退出 Sub
Else
pwd = tbInput.Text
结束 < span class =code-keyword>如果


如果 ValidatePassword(pwd)然后 ' = true
Form2.Show()

其他
Form4.Show()
结束 如果

如果 ValidatePassword(pwd)那么
Form3.Show()

Else
Form4.Show()
< span class =code-keyword>结束 如果

' 如果ValidatePassword(pwd)= True则
' Form2.Show()
' ElseIf ValidatePassword(pwd)= False然后
' Form4.Show ()
' 结束如果

< span class =code-comment>' 这只输出到另一个texbox true或false
Dim 输出 As String = ValidatePassword(pwd).ToString
tbOutput。文字=输出

结束 Sub





如果通过它将打开表格2和3

如果失败则只打开表格4一次。



传回一个布尔值对于测试是可以的,但是它没有告诉它用户他们失败了什么部分。

另外在最终版本中你可能想要添加更改值而不是使用默认值的功能。

如果你是想要检查输入的文本,你可以使用

 TextBox1_TextChanged 



并传递值在textbox1.text。

你仍然需要某种形式的反馈来解决不正确的问题。

你可能会考虑使用错误提供者。

http://msdn.microsoft.com/en -us / library / vstudio / a0d996e0(v = vs.100).aspx [ ^ ]



或其他形式的反馈



此外,字符串AAaa11使用默认值传递为有效,这不是一个好的密码。



I希望有所帮助


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:


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

解决方案

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

If ValidatePassword(TextBox1.Text) Then
Form2.Show()
Else
Form4.Show()
End If


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.

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:

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


这篇关于密码检查器visual basic 2010帮助?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆