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.
Mid went out with VB6 although it is still included for backward compatibility. Use SubString in new code.
Those Do loops are begging for endless loops if the User doesn't comply or gives up.
Your password rules would be helpful to a hacker. Are you salting and encrypting before storage?
Instead of an InputBox
use a TextBox
. Then you can use the Validating
event to test the password.
Fortunately a String
is an array of Char
. The Char
structure has many methods to determine what kind of character it is.
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? 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.
ButtonClicK:
Dim errorMessage as string = String.Empty
If ValidatePassword(textbox1.text, ErrorMessage) = False then msgbox.Show(errorMessage)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.