15,922,007 members
See more:
I have the following password rules. I need regular expression for this.

b.Minimum Numeric Character is 1
c.Minimum Alphabet Character is 2
d.Any Special Characters can access

Theigi Win
Posted
Theingi Win 15-Jun-11 23:49pm
Best Regards,
Theingi Win

## Solution 3

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!

## Solution 2

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; }

}

Theingi Win 15-Jun-11 23:48pm
Thanks! Rajesh Lagria

## Solution 1

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
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