C# .NET - password strength in windows forms application

Asked By kiran Kumar on 17-Nov-11 10:40 AM
hi,

  in my windows forms application project i have placed 2 textbox controls, one for user name and other for password like that

  i want to display password strength to the 2nd textbox

  that mean, if i enter less than 8 characters, it will have to show the password strength(weak, strong) like that.

 How to do this? i don't have installed ajax control toolkit , and i am new to this, so give me solution
Kirtan Patel replied to kiran Kumar on 17-Nov-11 10:45 AM
public enum PasswordScore
{
  Blank = 0,
  VeryWeak = 1,
  Weak = 2,
  Medium = 3,
  Strong = 4,
  VeryStrong = 5
}
 
public class PasswordAdvisor
{
  public static PasswordScore CheckStrength(string password)
  {
    int score = 1;
 
    if (password.Length < 1)
      return PasswordScore.Blank;
    if (password.Length < 4)
      return PasswordScore.VeryWeak;
    if (password.Length >= 6)
      score++;
    if (password.Length >= 12)
      score++;
    if (Regex.IsMatch(password, @"\d+"))
      score++;
    if (Regex.IsMatch(password, @"[a-z]") &&Regex.IsMatch(password, @"[A-Z]"))
      score++;
    if (Regex.IsMatch(password, @"[!@#\$%\^&\*\?_~\-\(\);\.\+:]+"))
      score++;
    return (PasswordScore)score;
  }
}
 
dipa ahuja replied to kiran Kumar on 17-Nov-11 10:49 AM
Implement the textChange Event:

private void textBox1_TextChanged(object sender, EventArgs e)
{
  if (textBox1.Text.Length < 5)
  {
    lblpw.Text = "Weak";
    lblpw.ForeColor = System.Drawing.Color.Red;
  }
  else if (textBox1.Text.Length >= 5 && textBox1.Text.Length < 8)
  {
    lblpw.Text = "Average";
    lblpw.ForeColor = System.Drawing.Color.Gray;
  }
  else if (textBox1.Text.Length >= 8)
  {
    lblpw.Text = "Strong";
    lblpw.ForeColor = System.Drawing.Color.Green;
  }
}







Neha Garg replied to kiran Kumar on 17-Nov-11 11:38 AM
Hello Kiran,

try this...

<form method="post" action="" id="user_registration" name="user_registration">
        <p><h1>Password strength metter</h1></p>
        <p>   
        <label for="user">Username</label><input type="text" name="user" id="user"/>
        </p>
        <p>   
        <label for="name">Name</label><input type="text" name="name" id="name"/>
        </p>
        <p>   
        <label for="surname">Surname</label><input type="text" name="surname" id="surname"/>
        </p>
        <p>   
        <label for="email">E-mail</label><input type="text" name="email" id="email"/>
        </p>
        <p>   
            <label for="pass">Password</label><input type="password" name="pass" id="pass" onkeyup="passwordStrength(this.value)" />
        </p>
        <p>   
        <label for="pass2">Confirm Password</label><input type="password" name="pass2" id="pass2"/>
        </p>
        <p>
            <label for="passwordStrength">Password strength</label>
            <div id="passwordDescription">Password not entered</div>
            <div id="passwordStrength" class="strength0"></div>
        </p>
        <p>   
        <input type="submit" name="submit" id="submit" value="Register">
        </p>
</form>
  
The css code
  
#passwordStrength
  
{
    height:10px;
    display:block;
    float:left;
}
.strength0
{
    width:250px;
    background:#cccccc;
}
.strength1
{
    width:50px;
    background:#ff0000;
}
.strength2
{
    width:100px;  
    background:#ff5f5f;
}
.strength3
{
    width:150px;
    background:#56e500;
}
.strength4
{
    background:#4dcd00;
    width:200px;
}
.strength5
{
    background:#399800;
    width:250px;
}
  
And finally the javascript code
  
function passwordStrength(password)
  
{
    var desc = new Array();
    desc[0] = "Very Weak";
    desc[1] = "Weak";
    desc[2] = "Better";
    desc[3] = "Medium";
    desc[4] = "Strong";
    desc[5] = "Strongest";
    var score   = 0;
  
    //if password bigger than 6 give 1 point
    if (password.length > 6) score++;
    //if password has both lower and uppercase characters give 1 point  
    if ( ( password.match(/[a-z]/) ) && ( password.match(/[A-Z]/) ) ) score++;
  
    //if password has at least one number give 1 point
  
    if (password.match(/\d+/)) score++;
  
    //if password has at least one special caracther give 1 point
  
    if ( password.match(/.[!,@,#,$,%,^,&,*,?,_,~,-,(,)]/) ) score++;
  
    //if password bigger than 12 give another 1 point
  
    if (password.length > 12) score++;
  
     document.getElementById("passwordDescription").innerHTML = desc[score];
  
     document.getElementById("passwordStrength").className = "strength" + score;
  
}