2016-06-20 5 views
0

私はこのコードを持っている:メッセージボックス

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桁の文字で構成する必要があります。

誰でも助けてください。ありがとう!

答えて

0

List(Of String)を使用して、発生した各エラーを格納します。

すべてのチェックが完了したら、Listに0個以上の項目がある場合は、エラーをメッセージボックスに表示する必要があります。それ以外の場合は、テストに合格しました。ここで

は、いくつかのサンプルコードです:

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 
+0

これは素晴らしいアイデアですが、私はこれらのエラーを得た:「Countは」「System.Windows.Forms.Message」と名「結果」のメンバではありませんです宣言されていない。 –

+0

私はvb.net winformsプロジェクトでこのコードを実行しましたが、エラーなしで実行されました。名前の競合を避けるために、別の変数名を使用するようにコードを更新しました。多分それをもう一度試してみてください。 –

+0

素晴らしい!どうもありがとうございました –

関連する問題