-1

I need a validate that the user need to put a minimum, 1 upper, 1 lower and 1 special character... I just put a validate that the user need minimum 6 letter or more...

try
{
    string actualPass = this.txtactualPass.Text;
    string newPass = this.txtnewPass.Text;
    string reEnterPass = this.txtReEnterPass.Text;

    if (actualPass == this.Session["password"].ToString())
    {
        if (newPass.Length >= 6)
        {
Code Maverick
  • 20,171
  • 12
  • 62
  • 114
Mikasuki
  • 35
  • 1
  • 8
  • What have you tried ? Also, I would suggest defining a password validation method in a library somewhere so that you can use the same method when the user subscribes or when he changes his password. – Laurent S. Nov 20 '13 at 17:21
  • Possible duplcate: http://stackoverflow.com/questions/9477906/password-must-be-8-characters-including-1-uppercase-letter-1-special-character – Babblo Nov 20 '13 at 17:21

2 Answers2

6

Instead of Special character you can check if your password contains digit. May be for special character you can use char.IsSymbol

if 
(
    newPass.Length >= 6 &&        //if length is >= 6
    newPass.Any(char.IsUpper) &&  //if any character is upper case
    newPass.Any(char.IsSymbol)    //better to have a digit then Symbol
)  
{
    //valid
}
Code Maverick
  • 20,171
  • 12
  • 62
  • 114
Habib
  • 219,104
  • 29
  • 407
  • 436
  • 3
    I definitely need to move on to LINQ :-) each time I see a nice answer like this one on SO, it refers to a LINQ functionality... – Laurent S. Nov 20 '13 at 17:24
  • @Bartdude - It's not always faster, but it's definitely prettier! – Code Maverick Nov 20 '13 at 17:25
  • @user3014210, you don't need library for `Length`, its a property of string, for `Any`, you need to include `using System.Linq;`, I hope you are targeting framework 3.5 or higher – Habib Nov 20 '13 at 18:12
  • @Scott - is not validating. – Mikasuki Nov 20 '13 at 19:08
  • @user3014210 - you mean Habib, not me =D – Code Maverick Nov 20 '13 at 19:12
  • @Scott - This is not right: (newPass.Length ((?=.*\d)(?=.*[A-Z])(?=.*\W).{8,8}) – Mikasuki Nov 20 '13 at 19:54
  • 1
    @user3014210, I am not sure why you keep tagging Scott in your comments, What are you trying to do now ? are you trying to compare your `newPass` with Regex, and why the code in my answer doesn't satisfy your conditions ? – Habib Nov 20 '13 at 20:02
  • @Habib - sorry about that, ok your code is validate: newPass.Length >= 6 && newPass.Any(char.IsUpper)...... but not validating newPass.Any(char.IsSymbol)... what can I do for validate the symbols?? – Mikasuki Nov 20 '13 at 20:08
  • @user3014210, what do you mean by symbols ? What are the characters you want the password to have ? – Habib Nov 20 '13 at 20:09
  • @Habib - Specials character like: !@#$%^&* – Mikasuki Nov 20 '13 at 20:13
  • @user3014210, you can have them in character array and then check using `IndexOfAny`, see [this answer](http://stackoverflow.com/questions/2522933/how-to-find-whether-a-string-contains-any-of-the-special-characters) – Habib Nov 20 '13 at 20:16
1

I understand this is very old thread, but for those who looking for the similar scenario. I would go for regular expression.

var result = "Password@123";

var expectedPasswordPattern = new Regex(@"^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$");

var isValidPassword = expectedPasswordPattern.IsMatch(result);    

Console.WriteLine(isValidPassword); 

Referred from here

Subodh Wasankar
  • 309
  • 2
  • 6