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



我是编程初学者。我们使用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 位数字? 谢谢

您可以添加一个布尔方法 验证密码 如下所述,并传递输入的密码。验证数字函数将使用正则表达式。由于您将只检查最小长度,一个字母字符和一个数字,我们将使用两个正则表达式作为字母的 ['a-z'、'A-Z'] 和数字的 ['0-9']。

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

最新更新