简体   繁体   English

我如何验证这一点?

[英]How do I validate this?

I have made this validation and I know it is very inefficient but the validation isn't completely working.我已经进行了此验证,我知道它的效率非常低,但验证并没有完全起作用。 What I mean is, when I was testing it I realised if I fail the first condition, then fix it, then fail the second condition, then when I fix the input to work with the second condition, I can make the input not correct for the first condition but it still passes through.我的意思是,当我测试它时,我意识到如果我不符合第一个条件,然后修复它,然后不符合第二个条件,然后当我修复输入以使用第二个条件时,我可以使输入不正确第一个条件,但它仍然通过。 I might have explained that very wrong but here is my code:我可能已经解释得很错误,但这是我的代码:

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

If you recognise this problem or you can help me out in my situation please respond.如果您认识到这个问题,或者您可以在我的情况下帮助我,请回复。

you can use single validation blog for both the validation.您可以使用单个验证博客进行验证。 Since I am using mobile to answer please ignore the mess with the format.由于我使用手机来回答,请忽略格式的混乱。

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

Perhaps you could do something along these lines...也许你可以按照这些思路做一些事情......

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

Or a simpler version could be made from something like...或者一个更简单的版本可以由类似...

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

What are you returning from your password?你从你的密码返回什么? You must declare a type for your function.您必须为您的 function 声明一个类型。

Mid went out with VB6 although it is still included for backward compatibility. Mid 与 VB6 一起推出,尽管它仍然包含在内以实现向后兼容性。 Use SubString in new code.在新代码中使用 SubString。

Those Do loops are begging for endless loops if the User doesn't comply or gives up.如果用户不遵守或放弃,那些 Do 循环正在乞求无限循环。

Your password rules would be helpful to a hacker.您的密码规则将对黑客有所帮助。 Are you salting and encrypting before storage?您是否在存储前加盐和加密?

Instead of an InputBox use a TextBox .而不是InputBox使用TextBox Then you can use the Validating event to test the password.然后您可以使用Validating事件来测试密码。

Fortunately a String is an array of Char .幸运的是String是一个Char数组。 The Char structure has many methods to determine what kind of character it is. 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

They teaching you vb6 stuff?他们教你vb6的东西? Show your teacher/professor the following:向您的老师/教授展示以下内容:

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

I don't know why you have to be in an endless loop like that.我不知道为什么你必须处于这样的无限循环中。 You pass this function in your button.您在按钮中传递此 function。

ButtonClicK:按钮点击:

Dim errorMessage as string = String.Empty
If ValidatePassword(textbox1.text, ErrorMessage) = False then msgbox.Show(errorMessage)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM