0

I want to create a login form with password strength meter ... Please help me.

Edited by Reverend Jim: Fixed formatting

2
Contributors
1
Reply
3
Views
7 Years
Discussion Span
Last Post by sknake
0

You need to define logic for how you would score the password strength but here is an example:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace daniweb.pwstrength
{
  public partial class Form1 : Form
  {
    const int PASS_MIN = 0;
    const int PASS_MAX = 10;
    const string PASS_STRENGTH_FORMAT = "{0:F0}/{1}";

    public Form1()
    {
      InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
      progressBar1.Minimum = PASS_MIN;
      progressBar1.Maximum = PASS_MAX;
      progressBar1.Value = 0;
      UpdateProgressBar();
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
      UpdateProgressBar();
    }

    private void UpdateProgressBar()
    {
      int strength = RatePassword(textBox1.Text);
      labelStrength.Text = string.Format(PASS_STRENGTH_FORMAT, strength, PASS_MAX);
      progressBar1.Value = strength;

      if (progressBar1.Value < 5)
        progressBar1.ForeColor = Color.Maroon;
      else
        progressBar1.ForeColor = Color.DarkGreen;
    }


    private static int RatePassword(string password)
    {
      if (string.IsNullOrEmpty(password))
        return 0;

      int result = Math.Min(password.Length, 7); //max 8/10 for length

      //+1 for a letter
      if (System.Text.RegularExpressions.Regex.Match(password, "[a-zA-Z]").Success)
        result += 1;
      //+1 for a number
      if (System.Text.RegularExpressions.Regex.Match(password, "[0-9]").Success)
        result += 1;
      //+1 for a special char
      if (System.Text.RegularExpressions.Regex.Match(password, "[\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)\\{\\}\\[\\]\\:\\'\\;\\\"\\/\\?\\.\\>\\,\\<\\~\\`\\-\\\\_\\=\\+\\|]").Success)
        result += 1;
      return result;
    }
  }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.