Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8679275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:55:52+00:00 2026-06-12T20:55:52+00:00

Possible Duplicate: Strong password regex Need RegEx for password strength? I was just wondering

  • 0

Possible Duplicate:
Strong password regex
Need RegEx for password strength?

I was just wondering what the best way to search a string for certain criteria (password strength to be specific) could be accomplished.

So far I have a simple:

if(password.Length <= 7)
    {
        errorMessage = "Your password must be at least 8 characters.";
    }

I would like to be able to check for capital letters, but I am not sure what the method or procedure is. I have tried Googling, searching the website: http://msdn.microsoft.com, and searching the index of my C# book (C# Programming 3E, by Barbara Doyle), but I can’t seem to find any.

I know I could try this…:

foreach(char c in password)
    {
        if(c!='A' || c!='B' || c!='C' || c!='D' ..... || c!='Z')
        {
            errorMessage = "Your password must contain at least one capital letter";
        }
    }

…But that would be extremely sloppy, and would have to be doubled to check for at least one lowercase letter. I am sure there is a better way to do this, or at least shorthand for the method I have shown above.

Also, I may decide to check the password for special characters (seems easier to do in the example above than with upper and lower case letters, so I may just use that for special characters, should I decide to make them necessary). If there is an easy (or proper) way to do that, I would love to have that knowledge, as well.

Anyway, thank you so much for any help anyone can give.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T20:55:53+00:00Added an answer on June 12, 2026 at 8:55 pm

    I can’t take the credit, as I stole this from here

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

    Note the use of regex for checking for upper case characters.
    This appears to be a decent approach, as it checks length, use of upper and lower case characters, numeric digits and special characters.

    ** Update **

    I know the question is now closed, but I can add more explanation for VoidKing to understand some of the concepts.

    A PasswordScore is returned from the CheckStrength method, which can be used as the condition for what to do next in your code.

    Here’s an untested demo of how the above code could be used:

    String password = "MyDummy_Password"; // Substitute with the user input string
    PasswordScore passwordStrengthScore = PasswordAdvisor.CheckStrength(password);
    
    switch (passwordStrengthScore) {
        case PasswordScore.Blank:
        case PasswordScore.VeryWeak:
        case PasswordScore.Weak:
                // Show an error message to the user
                break;
        case PasswordScore.Medium:
        case PasswordScore.Strong:
        case PasswordScore.VeryStrong:
               // Password deemed strong enough, allow user to be added to database etc
               break;
    }
    

    Enums are used in this case as a means of classifying the strength of the password into human-readable groups. Keeps the code clean, and makes it obvious what is going on in the code.

    Regarding the use of Regex’s, if you’re unfamiliar with the concept of them and how and when to use them, I suggest doing some research as these can be useful in many different scenarios for checking for patterns in strings. Perhaps start here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Why is string concatenation faster than array join? Usually we need to
Possible Duplicate: Dollar ($) sign in password string treated as variable I am using
Possible Duplicate: std::string and its automatic memory resizing I am just curious, how are
Possible Duplicate: PHP 2-way encryption: I need to store passwords that can be retrieved
Possible Duplicate: What is the best way to convert a java object to xml
Possible Duplicate: String equality vs equality of location I just got my first assessed
Possible Duplicate: Best way to prevent SQL injection? For logging in: $username = mysql_real_escape_string(htmlspecialchars(strip_tags(trim($_POST['username'])),
Possible Duplicate: Escape string for use in Javascript regex I have a msg like
Possible Duplicate: Best way to prevent SQL Injection in PHP What is the best
Possible Duplicate: Best way to use PHP to encrypt and decrypt? For my project

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.