Click here to Skip to main content
14,936,036 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a regular expression

((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20}) which validates the password.

and the description is

( # Start of group
(?=.*\d) # must contains one digit from 0-9
(?=.*[a-z]) # must contains one lowercase characters
(?=.*[A-Z]) # must contains one uppercase characters
(?=.*[@#$%]) # must contains one special symbols in the list "@#$%"
. # match anything with previous condition checking
{6,20} # length at least 6 characters and maximum of 20
) # End of group

But my requirement is to validate any three in four conditions.
Could anyone help me in finding this.

*** need to validate at time of entering the details.

1 solution

Here is the simple idea: create three separate regular expressions, validate the input password four time against each of them and calculate the candidate password's score: how many non-failures? The password is good if number of non-failures is 3 or greater. That's it.


Something like that:

System.Text.RegularExpressions.Regex[] criteria = new System.Text.RegularExpressions[] {
   new System.Text.RegularExpressions.Regex("..."),
   new System.Text.RegularExpressions.Regex("..."),
   new System.Text.RegularExpressions.Regex("..."), //use first 3 criteria you already have

bool IsValidPassword(string value) {
   int len = value.Length;
   if (value < 6) || (value > 20) return false;
   byte score = 0;
   for (System.Text.RegularExpressions.Regex criterion in criteria)
      if (criterion.IsMatch(value)
   return score > 2;

qwerty 2 19-Mar-12 1:19am
hi SAk

this would get me the output on clicking the button but i want it at the time of entry..
In case if you have given me the idea with the same please detail it..
Thanks in advance
No. This is the same. What do you mean "at the time of entry". You get entered value, use it in some predicate function bool IsValidPassword(string). Inside this function, you check 4 condition one by one; and the result: return score > 2;. That's it. How it can be different?
qwerty 2 19-Mar-12 2:45am
hey SAK...
I have tried it ..but not able to get..
Can you post me the code..??
(Sigh...) Please see the update above, after [EDIT].
qwerty 2 19-Mar-12 3:19am
I have created a method already and validated but i want this to work as client side validation not after any click event...
public bool ValidatePassword()
bool result=true;
if (TxtPwd.Text != null)
string pass = TxtPwd.Text;

bool isDigit = false;
bool isLetter = false;
bool isLowerChar = false;
bool isUpperChar = false;
bool isNonAlpha = false;

if (pass.Length >= 8)

foreach (char c in pass)
if (char.IsDigit(c))
isDigit = true;
if (char.IsLetter(c))
isLetter = true;
if (char.IsLower(c))
isLowerChar = true;
if (char.IsUpper(c))
isUpperChar = true;
Match m = Regex.Match(c.ToString(), @"\W|_");
if (m.Success)
isNonAlpha = true;

if ((isDigit && isLetter && isLowerChar && isUpperChar && isNonAlpha) || (!isNonAlpha && isLetter && isLowerChar && isUpperChar && isDigit) || (!isDigit && isLetter && isLowerChar && isUpperChar && isNonAlpha) || (isDigit && isLetter && !isLowerChar && isUpperChar && isNonAlpha) || ((isDigit && isLetter && isLowerChar && !isUpperChar && isNonAlpha)))
result = true;

result = false;

result = true;
return result;
I don't know why doing it this way. Please see my answer and do it this way, only put your own regular expression. Run it under debugger to see what happens.
qwerty 2 19-Mar-12 3:30am
yeah but i want it for client side validation i.e when i enter password and go for next tab it should show me an error message if i have entered with only two combinations..
You tell me now! First, why not going with server-side validation? It could be safer.
Secondly, JavaScript also has Regular Expressions. Use exact same algorithm, translated to JavaScript.
Look, I don't feel I have enough energy to write the same code in different language...

I can see that you can do it by yourself...

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