如何检查密码在Visual Basic中是否包含至少1个数字?

时间:2017-04-16 08:57:50

标签: visual-studio

我是编程的初学者。我们使用VISUAL BASIC语言

1)密码长度至少为6个字符 2)密码应包含至少一个数字和至少一个字母字符。如果密码包含至少1个数字数字,我如何检查?我写了这段代码:

Function IsValid(input As String) As Boolean
    input = input.Trim()

    If input.Length < 6 OrElse IsNumeric(input) Then
        MessageBox.Show("Your password should be at least 6 characters long,
        contain at least one numeric digit and at least one alphabetic character")
        Return False
    End If
    Return True
End Function

Private Sub btnCheck_Click(sender As Object, e As EventArgs) Handles btnCheck.Click
    If IsValid(txtInput.Text) Then
        MessageBox.Show("Thank you for creating your new password.")
    End If
End Sub

如果密码包含至少1个数字数字,我该如何检查? 感谢

1 个答案:

答案 0 :(得分:1)

您可以添加一个布尔方法ValidatePassword,如下所示,并输入密码。要验证数字,函数将使用正则表达式。由于您将仅检查最小长度,一个字母字符和一个数字数字,我们将使用两个正则表达式作为[&#39; a-z&#39;,&#39; A-Z&#39;]用于字母和[&#39; 0-9&#39;]代表数字。

Function ValidatePassword(ByVal pwd As String, Optional ByVal minLength As Integer = 6, Optional ByVal numNumbers As Integer = 1, Optional ByVal numAlphabet As Integer = 1) As Boolean
    Dim number As New System.Text.RegularExpressions.Regex("[0-9]")
    Dim alphabet As New System.Text.RegularExpressions.Regex("[A-Z],[a-z]")
    ' Check the length.
    If Len(pwd) < minLength Then Return False
    ' Check for minimum number of occurrences.
    If number.Matches(pwd).Count < numNumbers Then Return False
    ' Check for minimum number of occurrences.
    If alphabet.Matches(pwd).Count < numLower Then Return False
    ' Passed all checks.
    Return True
End Function

在此处http://www.sourcecodester.com/tutorials/visual-basic-net/6828/vbnet-password-complexity.html

完整检查复杂密码