Click here to Skip to main content
15,887,812 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
the below code is not working .whzat is the correct regular exp for given validations
-atleast one uppercase
-one special character
-length is 8.

What I have tried:

<pre>  private void textBox1_TextChanged(object sender, EventArgs e)
        {

            if (string.IsNullOrEmpty(textBox1.Text))
            {
                
                errorProvider1.SetError(textBox1, "Password required!");
            }
            else if (!Regex.IsMatch(textBox1.Text, "^.*(?=.{10,})(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$"))
            {
                errorProvider1.SetError(textBox1, "password must upeercase and lower case letters with maximum limit 6");
            }
            else
            {
                errorProvider1.SetError(textBox1, null);
            }
        }
Posted
Updated 14-Apr-17 21:42pm
Comments
LLLLGGGG 15-Apr-17 3:25am    
http://regexlib.com/REDetails.aspx?regexp_id=1111

1 solution

Don't use a Regex to do it all - they are too complex to maintain when password rules change.
Instead, do it in C# code:
C#
private string SpecialsAllowed = "!£$%^&*()_-=+.,";
public bool IsValidPassword(string pw)
    {
    if (string.IsNullOrWhiteSpace(pw)) return false;
    pw = pw.Trim();
    if (pw.Length < 8) return false;
    if (!pw.Any(c => char.IsUpper(c))) return false;
    if (!pw.Any(c => char.IsLower(c))) return false;
    if (pw.Intersect(SpecialsAllowed).Count() == 0) return false;
    return true;
    }
It's a lot clearer, and a lot simpler to modify when the rules change.

[edit]Union changed to Intersect :doh:[/edit]
 
Share this answer
 
v2
Comments
nv3 15-Apr-17 4:18am    
Didn't you mean pw.Intersect instead of pw.Union? Except for that I fully support your point.
OriginalGriff 15-Apr-17 4:26am    
Coffee ... I need more coffeeeeee ...

Fixed. :blush:

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