Hii all!!

Well i want to know about how to check for the password entered by the user in a textbox??

When a user enters some characters in textbox, i want to show a label which will tell about the strength of password, that is it strong, weak , poor...

can any one suggest me some code..

I had googled for it.. & found a class file, but on building that class file an error is occuring..

Errors are:

  1. Cannot implicitly convert type 'System.Text.RegularExpressions.Match' to 'bool'
  2. Operator '&&' cannot be applied to operands of type 'System.Text.RegularExpressions.Match' and 'System.Text.RegularExpressions.Match'

the class file is here::---

public class PasswordAdvisor
{
    enum PasswordScore
    {
        Blank = 0,
        VeryWeak = 1,
        Weak = 2,
        Medium = 3,
        Strong = 4,
        VeryStrong = 5
    }

    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.Match(password, @"/\d+/", RegexOptions.ECMAScript))
            score++;
        if (Regex.Match(password, @"/[a-z]/", RegexOptions.ECMAScript) &&
            Regex.Match(password, @"/[A-Z]/", RegexOptions.ECMAScript))
            score++;
        if (Regex.Match(password, @"/.[!,@,#,$,%,^,&,*,?,_,~,-,£,(,)]/", RegexOptions.ECMAScript))
            score++;

        return (PasswordScore)score;
    }
}

Hi, why don't you use the building password validation control provided in .NET, which I highly recommend and is easily customizable and quick 2. Anyway if you want to make your own custom one, here is basically what they where trying to do on the class you got, but mine doesn't have regular expressions on it, but you can easily do a method that returns either true or false after performing some reg exp validation and there are a million simple tutorials on regex on the internet. sorry can't give you a direct answer and plus I'm also busy struggling with my own asp problems.

class Program
    {
        /// <summary>
        /// just a simple password rating ratio, you can design a better one
        /// </summary>
        enum passWeight { empty = 0, invalid, weak, okay, good, secure };
        static void Main(string[] args)
        {
            string pass = "Majenge";
            Console.WriteLine(RatePassword(pass).ToString());
            Console.ReadKey();
        }

        /// <summary>
        /// Just a dummy method to show you how you can rate password, but no regular expressions are handled here, you can
        /// find a quick simple regular expression on the internet to search for your
        /// given criteria
        /// </summary>
        /// <param name="password">string holding password</param>
        /// <returns></returns>
        static passWeight RatePassword(string password)
        {
            try
            {
                int length = password.Length;

                if (length <= 0)
                {
                    return passWeight.empty;
                }
                else
                    if (length <= 3)
                    {
                        return passWeight.invalid;
                    }
                    else
                        if (length <= 5)
                        {
                            return passWeight.weak;
                        }
                        else
                            if (length <= 6)
                            {
                                return passWeight.okay;
                            }
                            else
                                if (length <= 7)
                                {
                                    return passWeight.good;
                                }
                                else
                                {
                                    return passWeight.secure;
                                }
            }
            catch(Exception ex)
            {
                throw new Exception();
            }

        }

       
    }

Hope this helps mate :)
Happy coding

Change to:

enum PasswordScore
        {
            Blank = 0,
            VeryWeak = 1,
            Weak = 2,
            Medium = 3,
            Strong = 4,
            VeryStrong = 5
        }

        private static PasswordScore CheckingPasswordStrength(string password)
        {
            int score = 1;
            if (password.Length < 1)
                return PasswordScore.Blank;
            if (password.Length < 4)
                return PasswordScore.VeryWeak;

            if (password.Length >= 8)
                score++;
            if (password.Length >= 12)
                score++;
            if (Regex.IsMatch(password, @"[0-9]+(\.[0-9][0-9]?)?", RegexOptions.ECMAScript))      //number only //"^\d+$" if you need to match more than one digit.
                score++;
            if (Regex.IsMatch(password, @"^(?=.*[a-z])(?=.*[A-Z]).+$", RegexOptions.ECMAScript)) //both, lower and upper case
                score++;
            if (Regex.IsMatch(password, @"[!,@,#,$,%,^,&,*,?,_,~,-,L,(,)]", RegexOptions.ECMAScript)) //^[A-Z]+$
                score++;
            return (PasswordScore)score;
        }

@ mshauny... hey thanks for the help..! can you please tell in detail about the .net own validation control...about which you were talking???

@ Mitja Bonca.. thanks a lot man!!! it worked.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.