C.PurS C.PurS - 5 months ago 12
Vb.net Question

Message box for multiple condition

I have this code:

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


it works well to give me message if one of the condition is fulfilled. I want to get something like:
For example, the input: askjf

then message box showed:


  1. Password must consist of 8 characters as minimum!

  2. Password must consist of uppercase letter!

  3. Password must consist of at least 1 digit character!



Can anybody help please? Thanks!

Answer

Use a List(Of String) to store each error as it is encountered.

After all the checks are done, if your List has more than zero items then you should show the errors in a message box. Otherwise, the test was passed.

Here's some sample code:

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