Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi
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.
Posted

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.

[EDIT]

Something like that:

C#
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)
         ++score;
   return score > 2;
}


—SA
 
Share this answer
 
v2
Comments
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
Sergey Alexandrovich Kryukov 19-Mar-12 1:29am    
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?
--SA
qwerty 2 19-Mar-12 2:45am    
hey SAK...
I have tried it ..but not able to get..
Can you post me the code..??
Sergey Alexandrovich Kryukov 19-Mar-12 3:15am    
(Sigh...) Please see the update above, after [EDIT].
--SA
qwerty 2 19-Mar-12 3:19am    
hmnn...
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;


}
else
{
result = false;

}
}
else
{
result = true;
}
return result;
}

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