2016-05-10 4 views
-1

Как вызвать функцию для проверки пароля, который был помещен в текстовое поле?Проверка пароля vb.net с использованием регулярных выражений

Function ValidatePassword(ByVal pwd As String, 
Optional ByVal minLength As Integer = 8, 
Optional ByVal numUpper As Integer = 2, 
Optional ByVal numLower As Integer = 2, 
Optional ByVal numNumbers As Integer = 2, 
Optional ByVal numSpecial As Integer = 2) 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 
If special.Matches(pwd).Count < numSpecial Then Return False 

' Passed all checks. 
Return True 
End Function 

ответ

0

Чтобы использовать эту функцию вам необходимо, чтобы захватить текст из текстового поля пароля и поместить его в функцию:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    Dim strPwd As String 
    strPwd = TextboxPassword.Text 'textbox containing password 
    If ValidatePassword(strPwd) = True Then 
     MessageBox.Show("Password is valid!") 
    Else 
     MessageBox.Show("Password is invalid. Please try again.") 
    End If 
End Sub 

Function ValidatePassword(ByVal pwd As String, 
     Optional ByVal minLength As Integer = 8, 
     Optional ByVal numUpper As Integer = 2, 
     Optional ByVal numLower As Integer = 2, 
     Optional ByVal numNumbers As Integer = 2, 
     Optional ByVal numSpecial As Integer = 2) 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 
    If special.Matches(pwd).Count < numSpecial Then Return False 

    ' Passed all checks. 
    Return True 
End Function 

Обратите внимание, что другие параметры не являются обязательными. Вам нужно будет только ввести текст пароля в функцию. Когда вы нажимаете Button1, код выполняет, в конечном итоге вызывает функцию ValidatePassword и отправляет окно сообщения, чтобы сообщить вам, был ли пароль действительным или нет.

Смежные вопросы