Click here to Skip to main content
15,891,674 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
hi

I have a requirement for the password to take any of the three options from four like.......
password must use a combination of at least three of the following four character types:
I.Atleast 1 upper case letters (A – Z)
II.Lower case letters (a – z)
III.Atleast 1 number (0 – 9)
IV.Atleast 1 non-alphanumeric symbol (e.g. @ ‘$%£!’)

So can anyone help me with the regular expression to be given in regular expression validator in asp.net or any other way where i can validate it?
Posted

A regex is a bad way to do this - this isn't a string matching operation, it's a character counting operation, and regexes are not good at that at all.
Do it in code: loop through each character and count it's groups. You can do this either server side as part of the postback testing or client side as part of the validator - it's up to you.
 
Share this answer
 
Hi,
it seems that it should implement on server side so call this method on your Button click:
C#
 bool ValidatePassword()
{
    string pass = txtpass.Text;

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

    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)
        result = true;

    return result;
}

hope this will help you.
 
Share this answer
 
v4
Comments
ProEnggSoft 24-Mar-12 23:31pm    
Edit: pre tag for C# code added - PEs
You should use a custom validater for this and not the regex validater. have a custom validater and validate it on client side to improve responsiveness(immediate feedback on wrong format) and validate it on server side to maintain security.
 
Share this answer
 

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