Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
As part of an assignment for my web-based vb class, we are now to design an application that asks the user to create a password and verify that it meets the criteria, which are as follows:

-The password should be at least six characters long.
-The password should contain at least one numeric digit and at least one alphabetic character.

I know that we are working with procedures and functions for this chapter, and I know what to do for the first criteria, but I'm not sure about the second part. This is not the kind of class I can get help from the teacher. What ways are there to check the second requirement? This is my code so far:


VB
Public Class Form1
    Dim strPassword As String

    Function IsValid(strPassword As String) As Boolean
        ' Check the user's password to see if it meets the
        ' requirements, and return a suitable message.

        If Not strPassword.Length >= 6 Then
            lblStatus.Text = "The password is too short. Please " &
            "include at least 6 characters."
            Return False
        End If

    End Function


    Private Sub btnAccept_Click(sender As Object, e As EventArgs) Handles btnAccept.Click
        ' Call the IsValid function to test the user's password.
        If IsValid(txtUserPassword.Text) = True Then
            lblStatus.Text = "The password meets the criteria."
        End If
    End Sub


    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        ' Terminate the application.
        Me.Close()
    End Sub
End Class


I can go back and enter the missing parts once I know what to do for the second requirement. Thank you for your time.
Posted
Comments
Sergey Alexandrovich Kryukov 8-Apr-15 12:11pm    
First of all, develop a separate Boolean function abstracted form any I/O.
—SA

People discussed many attempts to use regular expressions, but this is wrong. In such simple criteria as yours, this is simple.

Apparently, first thing is to check up string length. And then you have to have some local variable, byte score = 0; and accumulate the password score by its characters. Make a loop by all password characters and add to score if you encounter a digit, a letter, a punctuation character, and so on. Don't count score twice, have some Boolean flags (in simplest variant of this solution), such as hasLetter, hasDigit and do the check only if it does not have it yet.

Use the methods like System.Char.IsLetter, System.Char.IsNumber, System.Char.IsPunctuation or System.Char.IsLetterOrDigit, etc.

Please see: https://msdn.microsoft.com/en-us/library/system.char%28v=vs.110%29.aspx.

At the end, compare accumulated score with the required.

—SA
 
Share this answer
 
v2
Comments
CPallini 8-Apr-15 12:57pm    
5.
Sergey Alexandrovich Kryukov 8-Apr-15 14:24pm    
Thank you, Carlo.
—SA
Since this is for an assignment, I choose to only hint you into the right direction so you still have the chance to learn something by yourself :-)

If you should not be able to get to a working solution, I will help you with further hints.

Look into the static methods of the class Char:
https://msdn.microsoft.com/en-us/library/system.char.aspx[^]
 
Share this answer
 
Can Check on Client-Side using Regular Expression Validator.

try:

C#
(?=^.{12,25}$)(?=(?:.*?\d){2})(?=.*[a-z])(?=(?:.*?[A-Z]){2})(?=(?:.*?[!@#$%*()_+^&}{:;?.]){1})(?!.*\s)[0-9a-zA-Z!@#$%*()_+^&]*$


//=====================================================
(?=^.{12,25}$) -- password length range from 12 to 25, the numbers are adjustable 
 
(?=(?:.*?[!@#$%*()_+^&}{:;?.]){1}) -- at least 1 special characters (!@#$%*()_+^&}{:;?.}) , the number is adjustable 
 
(?=(?:.*?\d){2}) -- at least 2 digits, the number is adjustable 
 
(?=.*[a-z]) -- characters a-z 
 
  (?=(?:.*?[A-Z]){2}) -- at least 2 upper case characters, the number is adjustable 


See the Reference Here[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900