[英]How do I validate this?
我已经进行了此验证,我知道它的效率非常低,但验证并没有完全起作用。 我的意思是,当我测试它时,我意识到如果我不符合第一个条件,然后修复它,然后不符合第二个条件,然后当我修复输入以使用第二个条件时,我可以使输入不正确第一个条件,但它仍然通过。 我可能已经解释得很错误,但这是我的代码:
Function NewPassword(ByRef New_password As String)
Dim Valid1, Valid2 As Boolean
Dim First_char As String
Dim last_char As String
Dim First_char_asc As Integer
Dim last_char_asc As Integer
Valid1 = False
Valid2 = False
Do Until Valid1 = True And Valid2 = True
Do Until Valid1 = True
Valid1 = False
New_password = InputBox("New password needs a capital letter at the start, Re-enter a new password.")
First_char = Mid$(New_password, 1, 1)
First_char_asc = Asc(First_char)
If First_char_asc >= 65 Or First_char_asc <= 90 Then
Valid1 = True
End If
Loop
Do Until Valid2 = True
Valid2 = False
New_password = InputBox("New password needs a symbol at the end, Re-enter a new password.")
last_char = Mid$(New_password, Len(New_password), 1)
last_char_asc = Asc(last_char)
If last_char_asc >= 35 Or last_char_asc <= 37 Then
Valid2 = True
End If
Loop
If Valid1 = False Or Valid2 = False Then
Valid1 = False And Valid2 = False
End If
Loop
Return New_password
End Function
如果您认识到这个问题,或者您可以在我的情况下帮助我,请回复。
您可以使用单个验证博客进行验证。 由于我使用手机来回答,请忽略格式的混乱。
Function NewPassword(ByRef New_password As String)
Dim Valid1, Valid2 As Boolean
Dim First_char As String
Dim last_char As String
Dim First_char_asc As Integer
Dim last_char_asc As Integer
Valid1 = False
Valid2 = False
Do Until Valid1 = True
Valid1 = False
New_password = InputBox("New password needs a capital letter at the start and a symbol at the end. Re-enter a new password.")
First_char = Mid$(New_password, 1, 1)
First_char_asc = Asc(First_char)
last_char = Mid$(New_password, Len(New_password), 1)
last_char_asc = Asc(last_char)
If (First_char_asc >= 65 and First_char_asc <= 90) And (last_char_asc >= 35 and last_char_asc <= 37) Then
Valid1 = True
End If
Loop
Return New_password
End Function
也许你可以按照这些思路做一些事情......
Function ValidateCapital(New_password, valid1)
Dim First_char = Mid$(New_password, 1, 1)
Dim First_char_asc = Asc(First_char)
If First_char_asc >= 65 And First_char_asc <= 90 Then
valid1 = True
Else
valid1 = False
End If
Return valid1
End Function
Function ValidateSymbol(New_password, valid2)
Dim last_char = New_password(New_password.Length - 1)
Dim last_char_asc = Asc(last_char)
If last_char_asc >= 35 And last_char_asc <= 37 Then
valid2 = True
Else
valid2 = False
End If
Return valid2
End Function
Function NewPassword(ByRef New_password As String)
Dim Valid1, Valid2 As Boolean
Valid1 = ValidateCapital(New_password, Valid1)
Valid2 = ValidateSymbol(New_password, Valid2)
While Valid1 <> True Or Valid2 <> True
If Valid1 = False Then
New_password = InputBox("New password needs a capital letter at the start, Re-enter a new password.")
Valid1 = ValidateCapital(New_password, Valid1)
End If
If Valid1 = True Then
Valid2 = ValidateSymbol(New_password, Valid2)
If Valid2 = False Then
New_password = InputBox("New password needs a symbol at the end, Re-enter a new password.")
Valid2 = ValidateSymbol(New_password, Valid2)
End If
End If
Valid1 = ValidateCapital(New_password, Valid1)
Valid2 = ValidateSymbol(New_password, Valid2)
End While
Return New_password
End Function
或者一个更简单的版本可以由类似...
Sub Main()
Dim valid1 = False
Dim valid2 = False
While valid1 = False Or valid2 = False
Dim Password = InputBox(“Enter your password”)
Dim First_char = Asc(Mid$(Password, 1, 1))
If valid1 = True And (First_char >= 65 Or First_char <= 90) Then
valid1 = True
End If
Dim Last_char = Asc(Mid$(Password, Len(Password), 1))
If valid2 = True And (Last_char >= 35 Or Last_char <= 37) Then
valid2 = True
End If
End While
End Sub
你从你的密码返回什么? 您必须为您的 function 声明一个类型。
Mid 与 VB6 一起推出,尽管它仍然包含在内以实现向后兼容性。 在新代码中使用 SubString。
如果用户不遵守或放弃,那些 Do 循环正在乞求无限循环。
您的密码规则将对黑客有所帮助。 您是否在存储前加盐和加密?
而不是InputBox
使用TextBox
。 然后您可以使用Validating
事件来测试密码。
幸运的是String
是一个Char
数组。 Char
结构有很多方法可以确定它是什么类型的字符。
Private Sub TextBox1_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
Dim ValidString = ValidatePassword(TextBox1.Text)
If Not ValidString = "Valid" Then
MessageBox.Show(ValidString)
e.Cancel = True
End If
End Sub
Private Function ValidatePassword(New_password As String) As String
If Char.IsLower(New_password(0)) Then
Return "New password needs a capital letter at the start, Re-enter a new password."
End If
If Not Char.IsSymbol(New_password(New_password.Length - 1)) Then
Return "New password needs a symbol at the end, Re-enter a new password."
End If
Return "Valid"
End Function
他们教你vb6的东西? 向您的老师/教授展示以下内容:
Public Function ValidatePassword(Pw as string, byref ErrorMessage As String) As Boolean
ErrorMessage = ""
Dim FirstValidation As Boolean, SecondValidation As Boolean
Dim firstChar as char = Pw.SubString(0,1)
Dim lastChar as char = Pw.SubString(pw.length,1)
FirstValidation = asc(firstchar) >= 65 And asc(firstchar) <= 90
If FirstValidation = False Then ErrorMessage = "The first character is not Uppercase" & vbnewline
SecondValidation = asc(lastchar) >= 35 And last_char_asc <= 37
If SecondValidation = False Then ErrorMessage &= "The last character is not a symbol" & vbnewline
Return FirstValidation And SecondValidation
End Function
我不知道为什么你必须处于这样的无限循环中。 您在按钮中传递此 function。
按钮点击:
Dim errorMessage as string = String.Empty
If ValidatePassword(textbox1.text, ErrorMessage) = False then msgbox.Show(errorMessage)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.