Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Password Regex Help

Password Regex Help

Scheduled Pinned Locked Moved C#
csharpcomregexhelpquestion
8 Posts 7 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jassim Rahma
    wrote on last edited by
    #1

    Hi, How can I have a regex for my .NET app's user password with the following: Password MUST be a minimum of 10 AND maximum of 50 It should NOT contain space. It should be MIXED alphabet (upper + lower), numeric and following special characters !@#$%^&*-_+=~ Thanks, Jassim

    www.softnames.com

    D OriginalGriffO L U B 5 Replies Last reply
    0
    • J Jassim Rahma

      Hi, How can I have a regex for my .NET app's user password with the following: Password MUST be a minimum of 10 AND maximum of 50 It should NOT contain space. It should be MIXED alphabet (upper + lower), numeric and following special characters !@#$%^&*-_+=~ Thanks, Jassim

      www.softnames.com

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      RegEx is not a good fit for password rules. You'd be much better off coding each rule separately.

      Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
      Dave Kreskowiak

      1 Reply Last reply
      0
      • J Jassim Rahma

        Hi, How can I have a regex for my .NET app's user password with the following: Password MUST be a minimum of 10 AND maximum of 50 It should NOT contain space. It should be MIXED alphabet (upper + lower), numeric and following special characters !@#$%^&*-_+=~ Thanks, Jassim

        www.softnames.com

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        The problem with using a regex for this is that it gets very, very complex - and that means that when the rules change (and they always do) a horribly complicated and difficult to understand string has to be modified, tested, fixed, tested, and finally released. Which makes maintenance difficult and prone to error. Instead, use .NET string operations (or individual Regexes) to pick up each individual part:

        int numLoCase = ...
        int numUpCase = ...
        int numSpaces = ...
        int numNumeric = ...
        int numSpecial = ...
        int len = ...

        And then apply a single if statement to apply the rules:

        if (len >= 10
        && len <= 50
        && numSpaces == 0
        && ...

        You get much more readable code, and more reliable maintenence.

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        1 Reply Last reply
        0
        • J Jassim Rahma

          Hi, How can I have a regex for my .NET app's user password with the following: Password MUST be a minimum of 10 AND maximum of 50 It should NOT contain space. It should be MIXED alphabet (upper + lower), numeric and following special characters !@#$%^&*-_+=~ Thanks, Jassim

          www.softnames.com

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Jassim Rahma wrote:

          Password MUST be a minimum of 10 AND maximum of 50 It should NOT contain space. It should be MIXED alphabet (upper + lower), numeric and following special characters !@#$%^&*-_+=~

          Tell management to fuck off. A horse staple is better. Let's do a Zoom where I can explain that fine detail. I will be recording for training purposes only.

          Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

          1 Reply Last reply
          0
          • J Jassim Rahma

            Hi, How can I have a regex for my .NET app's user password with the following: Password MUST be a minimum of 10 AND maximum of 50 It should NOT contain space. It should be MIXED alphabet (upper + lower), numeric and following special characters !@#$%^&*-_+=~ Thanks, Jassim

            www.softnames.com

            U Offline
            U Offline
            User 11526936
            wrote on last edited by
            #5

            probe the next function:

            bool IsValidPassword(string password,
            int minLength=10,
            int maxLength=50,
            bool requireLowercase=true,
            bool requireUppercase=false,
            bool requireNumber=false,
            bool requireSimbol=false)
            {
            if (string.IsNullOrEmpty(password))
            {
            return false;
            }
            if (password.Length < minLength || password.Length > maxLength)
            {
            return false;
            }
            if (requireUppercase && !Regex.IsMatch(password, "[A-Z]"))
            {
            return false;
            }
            if (requireLowercase && !Regex.IsMatch(password, "[a-z]"))
            {
            return false;
            }
            if (requireNumber && !Regex.IsMatch(password, "[0-9]"))
            {
            return false;
            }
            string pattern= @"[!@#$%^&*()_+\-=\[\]{};':" + "\"" + @"\\|,.<>\/? ~]";
            if (requireSimbol && !Regex.IsMatch(password, pattern))
            return false;
            return true;
            }

            L 1 Reply Last reply
            0
            • U User 11526936

              probe the next function:

              bool IsValidPassword(string password,
              int minLength=10,
              int maxLength=50,
              bool requireLowercase=true,
              bool requireUppercase=false,
              bool requireNumber=false,
              bool requireSimbol=false)
              {
              if (string.IsNullOrEmpty(password))
              {
              return false;
              }
              if (password.Length < minLength || password.Length > maxLength)
              {
              return false;
              }
              if (requireUppercase && !Regex.IsMatch(password, "[A-Z]"))
              {
              return false;
              }
              if (requireLowercase && !Regex.IsMatch(password, "[a-z]"))
              {
              return false;
              }
              if (requireNumber && !Regex.IsMatch(password, "[0-9]"))
              {
              return false;
              }
              string pattern= @"[!@#$%^&*()_+\-=\[\]{};':" + "\"" + @"\\|,.<>\/? ~]";
              if (requireSimbol && !Regex.IsMatch(password, pattern))
              return false;
              return true;
              }

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              What's a simbol?

              Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

              R 1 Reply Last reply
              0
              • L Lost User

                What's a simbol?

                Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                R Offline
                R Offline
                Ravi Bhavnani
                wrote on last edited by
                #7

                It's a symbol. /ravi

                My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

                1 Reply Last reply
                0
                • J Jassim Rahma

                  Hi, How can I have a regex for my .NET app's user password with the following: Password MUST be a minimum of 10 AND maximum of 50 It should NOT contain space. It should be MIXED alphabet (upper + lower), numeric and following special characters !@#$%^&*-_+=~ Thanks, Jassim

                  www.softnames.com

                  B Offline
                  B Offline
                  Bohdan Stupak
                  wrote on last edited by
                  #8

                  Apart from the above-mentioned problem with the readability of Regex, another problem is that Regex is quite computation-heavy, and due to the fact that it builds underlying state-machine[^] performance penalty is heavier for big strings.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups