VB密码检查器

时间:2016-01-14 18:21:00

标签: vb.net

我必须检查密码是否符合安全要求。我必须:

  • 检查长度是否至少为8个字符
  • 检查至少有一个数字
  • 检查是否至少有一个小写字符
  • 检查是否至少有一个大写字符

这是我到目前为止的代码

Module Module1
    Sub Main()
        Console.WriteLine("Welcome to the password checker")
        Console.WriteLine("Please enter an 8 digit password")
        Dim mypassword As String = Console.ReadLine()
        Console.WriteLine("Your password is, {0}", mypassword)
    End Sub

    Function Passwordlength(ByVal mypassword)
        mypassword.length()
        Console.WriteLine(mypassword.length)
        Console.ReadLine()
    End Function
End Module

2 个答案:

答案 0 :(得分:2)

如果字符串短于8个字符,则以下函数返回Any。然后,它使用False方法检查字符串中的所有字符,以查看它们中是否有特定类型。如果没有数字,小写字母或大写字母,则返回True。如果所有这些检查都通过,则返回Function ValidPassword(myPassword As String) As Boolean If myPassword.Length < 8 Then Return False If Not myPassword.Any(Function(c) Char.IsDigit(c)) Then Return False If Not myPassword.Any(Function(c) Char.IsLower(c)) Then Return False If Not myPassword.Any(Function(c) Char.IsUpper(c)) Then Return False Return True End Function

Dim myPassword As String = Console.ReadLine()
If ValidPassword(myPassword) Then
    Console.WriteLine("Your password is, {0}", mypassword)
Else
    Console.WriteLine("Password {0} is invalid", mypassword)
End If

你可以像这样调用这个函数

{{1}}

答案 1 :(得分:-2)

Function longEnough(strdata As String) As Boolean
    If Len(strdata) > 7 Then
        Return True
        Exit Function
    End If
End Function
Function HasNumber(strData As String) As Boolean
    Dim iCnt As Integer

    For iCnt = 1 To Len(strData)
        If IsNumeric(Mid(strData, iCnt, 1)) Then
            HasNumber = True
            Exit Function
        End If
    Next iCnt

End Function

Function IsUpper(strData As String) As Boolean
    Dim iCnt As Integer

    For iCnt = 1 To Len(strData)
        If Char.IsUpper(Mid(strData, iCnt, 1)) Then
            IsUpper = True
            Exit Function
        End If
    Next iCnt

End Function

用法:

Dim string1 As String = "hellothere"

        If longEnough(string1) = True And HasNumber(string1) = True And IsUpper(string1) = True Then
            MsgBox("you are good")
        Else
            MsgBox("you need a cap, upper, and be at least 8 characters")
        End If