5
Answers

Password Validator in C#

Photo of karthik v

karthik v

13y
1.6k
1
Hi Experts
I have a case where i need to validate the password for the following cases 
1) at least one lower case letter
2) at least one upper case letter
3) at least special character
4) at least one number

In the above conditions if any of the three conditions are satisfied then the password is okay .

Can any one help me with this issue?

Answers (5)

0
Photo of Vulpes
NA 98.3k 1.5m 13y
It's difficult for regular expressions to deal with stuff such as 'any three out four of conditions' using expressions of a reasonable length and you'd need alternators for each of the lookaheads in Jitendra's expression to do it properly.

If you don't need to do it using reglar expressions, then I'd do it using traditional string parsing which is relatively straightforward to do and executes quickly. I've assumed below that it's no problem if all four conditions are satisfied and not just any three:

using System;

class Test
{
   static void Main()
   {
      string[] passWords = {"aX2#", "sed2T", "*v3X", "Ae234&B", "fg234", "g1HL","#1$23", "5a7%"};

      foreach(string passWord in passWords)
      {
         bool b = ValidatePassword(passWord);
         Console.WriteLine("'{0}' is{1} a valid password", passWord, b ? "": "n't");
      }
 
      Console.ReadKey(); 
   }

   static bool ValidatePassword(string passWord)
   {
      int validConditions = 0;

      foreach(char c in passWord)
      {
         if (c >= 'a' && c <= 'z')
         {
            validConditions++;
            break;
         } 
      }

      foreach(char c in passWord)
      {
         if (c >= 'A' && c <= 'Z')
         {
            validConditions++;
            break;
         } 
      }

      if (validConditions == 0) return false;

      foreach(char c in passWord)
      {
         if (c >= '0' && c <= '9')
         {
            validConditions++;
            break;
         } 
      }

      if (validConditions == 1) return false;

      if(validConditions == 2)
      {       
         char[] special = {'@', '#', '$', '%', '^', '&', '+', '='}; // or whatever

         if (passWord.IndexOfAny(special) == -1) return false;
      }

      return true;
   }
Accepted
0
Photo of Vulpes
NA 98.3k 1.5m 13y
Like us all, I have my strengths and weaknesses but anything to do with strings is definitely a strength :)
0
Photo of Jiteendra Sampathirao
NA 6.9k 1.5m 13y

@vulpes: you are great...
0
Photo of karthik v
NA 109 56.2k 13y
Thank you the quick response guys.I used the C# code and that worked well.I tried to use the regex but it was a kind of complicating things.Thank you so much guys...
0
Photo of Jiteendra Sampathirao
NA 6.9k 1.5m 13y
Hi,
try this..

<table>
<tr>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="RegularExpressionValidator"
ValidationExpression="^.*(?=.{10,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$"
ControlToValidate="TextBox1"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>
<asp:Button ID="Button1" runat="server" Text="Button" />
</td>
</tr>
</table>

hope this help you...