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. simple PAssword Policy

simple PAssword Policy

Scheduled Pinned Locked Moved C#
regexcsharplinqgraphicshelp
5 Posts 4 Posters 0 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.
  • T Offline
    T Offline
    THE SK
    wrote on last edited by
    #1

    Hi All I am struggling to make a regular expresssion for the password policy. There are only two requiremnts for the password policy 1. Password should of length atleast 6 2. password must contain atleast two characters at any position. Match cases(for which regex should pass) are 1. rt5465465 2. 6556h76f 3. 76d12j 4. s45)$f Cases for which regex should fail are 1. w565765 2. 872310 3. 4r@%&9 4. gghj The regex whixh I have made so far are in the below code 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; using System.Text.RegularExpressions; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //Regex regex = new Regex("(.*[a-zA-Z].*[a-zA-Z])"); // Regex regex = new Regex("(?(^(?=.+[a-zA-Z$#%^&*()!@~+]){6,}))(?(.*[a-zA-Z].*[a-zA-Z]))(.{6,})"); Regex regex = new Regex("(^[.]*${6,})"); //Regex regex = new Regex("([a-zA-Z0-9]{6,})"); //Regex regex = new Regex("(?(?=[.]{6,})(.*[a-zA-Z].*[a-zA-Z])([Z][9][4][M][P]))"); // Regex regex = new Regex("(?(^(?=.+[a-zA-Z$#%^&*()!@~+]){6,}))(.*[a-zA-Z].*[a-zA-Z])"); // Regex regex = new Regex("(?(?=[0-9]{5,5}[a-z])([6])([1]{5,5}))"); // Regex regex = new Regex("(?(?=[0-9]{5,5}[a-z])()([1]{5,5}))"); //Regex regex = new Regex("(?(?=[a-zA-Z0-9]{6,})()(([1])([1])([p])))"); string s = textBox1.Text; if (regex.IsMatch(s)) { MessageBox.Show("pass"); } else { MessageBox.Show("fail"); } } } } Please help Thanks Regards Sandeep

    L realJSOPR 2 Replies Last reply
    0
    • T THE SK

      Hi All I am struggling to make a regular expresssion for the password policy. There are only two requiremnts for the password policy 1. Password should of length atleast 6 2. password must contain atleast two characters at any position. Match cases(for which regex should pass) are 1. rt5465465 2. 6556h76f 3. 76d12j 4. s45)$f Cases for which regex should fail are 1. w565765 2. 872310 3. 4r@%&9 4. gghj The regex whixh I have made so far are in the below code 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; using System.Text.RegularExpressions; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //Regex regex = new Regex("(.*[a-zA-Z].*[a-zA-Z])"); // Regex regex = new Regex("(?(^(?=.+[a-zA-Z$#%^&*()!@~+]){6,}))(?(.*[a-zA-Z].*[a-zA-Z]))(.{6,})"); Regex regex = new Regex("(^[.]*${6,})"); //Regex regex = new Regex("([a-zA-Z0-9]{6,})"); //Regex regex = new Regex("(?(?=[.]{6,})(.*[a-zA-Z].*[a-zA-Z])([Z][9][4][M][P]))"); // Regex regex = new Regex("(?(^(?=.+[a-zA-Z$#%^&*()!@~+]){6,}))(.*[a-zA-Z].*[a-zA-Z])"); // Regex regex = new Regex("(?(?=[0-9]{5,5}[a-z])([6])([1]{5,5}))"); // Regex regex = new Regex("(?(?=[0-9]{5,5}[a-z])()([1]{5,5}))"); //Regex regex = new Regex("(?(?=[a-zA-Z0-9]{6,})()(([1])([1])([p])))"); string s = textBox1.Text; if (regex.IsMatch(s)) { MessageBox.Show("pass"); } else { MessageBox.Show("fail"); } } } } Please help Thanks Regards Sandeep

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, I wouldn't know with Regex; this is what I would do:

      public bool IsValidPassword(string s) {
      if (s==null || s.Length<6) return false;
      int nLetters=0;
      foreach(char c in s) if (Char.IsLetter(c)) nLetters++;
      return nLetters>=2;
      }

      And someone is bound to come up with some more modern approach, probably using LINQ. BTW: I don't think your criteria are alright; I would also require at least 2 non-letters. :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


      T 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, I wouldn't know with Regex; this is what I would do:

        public bool IsValidPassword(string s) {
        if (s==null || s.Length<6) return false;
        int nLetters=0;
        foreach(char c in s) if (Char.IsLetter(c)) nLetters++;
        return nLetters>=2;
        }

        And someone is bound to come up with some more modern approach, probably using LINQ. BTW: I don't think your criteria are alright; I would also require at least 2 non-letters. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


        T Offline
        T Offline
        THE SK
        wrote on last edited by
        #3

        I need RegEx for this. Actually there is a web part (which is implemented using usercontrol) whose compiled dll is in GAC The designer code is in file. In web part username and paasword textbox is there There is also a login button on which the event which is in compiled assembliy in GAC get fired I cannot write code.I know its easy using code I am putting a regular expression validator in the designers code.So only through Regex things can be done Please help Thanks Regards THE SK

        1 Reply Last reply
        0
        • T THE SK

          Hi All I am struggling to make a regular expresssion for the password policy. There are only two requiremnts for the password policy 1. Password should of length atleast 6 2. password must contain atleast two characters at any position. Match cases(for which regex should pass) are 1. rt5465465 2. 6556h76f 3. 76d12j 4. s45)$f Cases for which regex should fail are 1. w565765 2. 872310 3. 4r@%&9 4. gghj The regex whixh I have made so far are in the below code 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; using System.Text.RegularExpressions; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //Regex regex = new Regex("(.*[a-zA-Z].*[a-zA-Z])"); // Regex regex = new Regex("(?(^(?=.+[a-zA-Z$#%^&*()!@~+]){6,}))(?(.*[a-zA-Z].*[a-zA-Z]))(.{6,})"); Regex regex = new Regex("(^[.]*${6,})"); //Regex regex = new Regex("([a-zA-Z0-9]{6,})"); //Regex regex = new Regex("(?(?=[.]{6,})(.*[a-zA-Z].*[a-zA-Z])([Z][9][4][M][P]))"); // Regex regex = new Regex("(?(^(?=.+[a-zA-Z$#%^&*()!@~+]){6,}))(.*[a-zA-Z].*[a-zA-Z])"); // Regex regex = new Regex("(?(?=[0-9]{5,5}[a-z])([6])([1]{5,5}))"); // Regex regex = new Regex("(?(?=[0-9]{5,5}[a-z])()([1]{5,5}))"); //Regex regex = new Regex("(?(?=[a-zA-Z0-9]{6,})()(([1])([1])([p])))"); string s = textBox1.Text; if (regex.IsMatch(s)) { MessageBox.Show("pass"); } else { MessageBox.Show("fail"); } } } } Please help Thanks Regards Sandeep

          realJSOPR Offline
          realJSOPR Offline
          realJSOP
          wrote on last edited by
          #4

          THE SK wrote:

          2. password must contain at least two characters at any position.

          Only Chuck Norris' passwords can do this.

          .45 ACP - because shooting twice is just silly
          -----
          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
          -----
          "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

          L 1 Reply Last reply
          0
          • realJSOPR realJSOP

            THE SK wrote:

            2. password must contain at least two characters at any position.

            Only Chuck Norris' passwords can do this.

            .45 ACP - because shooting twice is just silly
            -----
            "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
            -----
            "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

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

            :laugh:

            I are Troll :suss:

            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