Click here to Skip to main content
15,038,345 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following password rules. I need regular expression for this.

Please follow the following password rules:
a.Minimum Password Length is 8
b.Minimum Numeric Character is 1
c.Minimum Alphabet Character is 2
d.Any Special Characters can access

Theigi Win
Posted
Comments
Theingi Win 15-Jun-11 23:49pm
   
Thanks for your Answers! Now i solved it.
Best Regards,
Theingi Win

Try this:
C#
char[] cArray = textBox1.Text.ToCharArray();
int length = cArray.Length;
bool hasDigit = cArray.Any(c => char.IsDigit(c));
int alphabetCount = cArray.Count(c => char.IsLetter(c));
int specialCharCount = cArray.Count(c => !char.IsLetterOrDigit(c));


Hope this helps!
   
try this :-


C#
private void button1_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            string str = textBox1.Text.Trim();
            int noOfChr = 0;
            int noOfNo = 0;
            for (int i = 0; i < str.Length; i++)
            {
                if (char.IsLetter(str[i]))
                {
                    sb.Append(str[i]);
                    noOfChr = noOfChr + 1;

                }
                else if (char.IsDigit(str[i]))
                {
                    sb.Append(str[i]);
                    noOfNo = noOfNo + 1;

                }
            }
            if (noOfNo < 1) { MessageBox.Show("Enter minimum 1 Digit !!"); return; }
            if (noOfChr < 2) { MessageBox.Show("Enter minimum 2 Alphabet !!"); return; }


        }
   
Comments
Theingi Win 15-Jun-11 23:48pm
   
Thanks! Rajesh Lagria
I think Regular Expression is not the best tool here, because your requirements don't present any regular pattern; all the listed features can be mixed in all possible combinations.

The problem is easy-to-solve. Consider your requirements as pseudo-code and your 'a'-'d' items as separate predicates. Make your pseudo-code into real code by counting each feature. In this way, you could even return a string reporting all disadvantaged about user's password candidate to the user item by item. With Regular Expression this nice feature would be completely impossible.

Write this simple code and ask a question if you face any problems.

Good luck,
—SA
   
v2
Comments
Theingi Win 15-Jun-11 23:48pm
   
Thanks! SAKryukov
Sergey Alexandrovich Kryukov 17-Jun-11 22:37pm
   
You're very welcome.
If you find it informative or helpful, please formally accept it (green button).
Thank you.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900