多个条件的消息框

时间:2016-06-20 07:52:32

标签: vb.net if-statement login passwords

我有这段代码:

Class Form1
    Function ValidatePassword(ByVal pwd As String, Optional ByVal minLength As Integer = 8, Optional ByVal numUpper As Integer = 1, Optional ByVal numLower As Integer = 1, Optional ByVal numNumbers As Integer = 1) As Boolean
        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]")

        If Len(pwd) < minLength Then
            MsgBox("Password must consist of 8 characters as minimum!")
            Return False
        End If

        If upper.Matches(pwd).Count < numUpper Then
            MsgBox("Password must consist of uppercase letter!")
            Return False
        End If

        If lower.Matches(pwd).Count < numLower Then
            MsgBox("Password must consist of lowercase letter!")
            Return False
        End If

        If number.Matches(pwd).Count < numNumbers Then
            MsgBox("Password must consist of at least 1 digit character!")
            Return False
        End If

        MsgBox("Password OK!")
        Return True
    End Function
    Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Label3.Text = ValidatePassword(TextBox1.Text)
    End Sub

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

    End Sub
End Class

如果满足其中一个条件,它可以给我留言。我希望得到类似的东西: 例如,输入: askjf

然后消息框显示:

  1. 密码必须至少包含8个字符!
  2. 密码必须包含大写字母!
  3. 密码必须至少包含1个数字字符!
  4. 有人可以帮忙吗?谢谢!

1 个答案:

答案 0 :(得分:0)

使用List(Of String)存储遇到的每个错误。

完成所有检查后,如果您的List项目超过零,则应在消息框中显示错误。否则,测试通过。

以下是一些示例代码:

Function ValidatePassword(ByVal pwd As String, Optional ByVal minLength As Integer = 8, Optional ByVal numUpper As Integer = 1, Optional ByVal numLower As Integer = 1, Optional ByVal numNumbers As Integer = 1) As Boolean
    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]")

    'create a new list to store any errors
    Dim messageList As New List(Of String)
    'create a boolean to handle true/ false result
    Dim checkResult As Boolean

    'add messages as necessary depending on the checks
    If Len(pwd) < minLength Then
        messageList.Add("Password must consist of 8 characters as minimum!")
    End If

    If upper.Matches(pwd).Count < numUpper Then
        messageList.Add("Password must consist of uppercase letter!")
    End If

    If lower.Matches(pwd).Count < numLower Then
        messageList.Add("Password must consist of lowercase letter!")
    End If

    If number.Matches(pwd).Count < numNumbers Then
        messageList.Add("Password must consist of at least 1 digit character!")
    End If

    ' check for any error messages
    If messageList.Count = 0 Then
        MsgBox("Password OK!")
        checkResult = True
    Else
        'join the list items together using new line as separator for formatting
        MessageBox.Show(Join(messageList.ToArray, vbCrLf))
        checkResult = False
    End If

    'result of function
    Return checkResult

End Function