Password Validator in C#

Everyone may experience this requirement when we creating new password,

  1. At least one lower case letter,
  2. At least one upper case letter,
  3. At least special character,
  4. At least one number
  5. At least 8 characters length

So we can achieve this one in two ways

  1. Using regular expressions
  2. Using C# code

Using Regular Expressions

Refer this link for basics of Regular Expression Syntaxes

In Asp.net we have RegularExpressionValidator control..

  1. Use one text box with one button
  2. Declare the RequiredFieldValidator
  3. Declare the RegularExpressionvalidation
Important Properties of Regular Expression Validator:
  1. ErrorMessage
  2.  ValidationExpression
  3. ControlToValidate

This> "^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!*@#$%^&+=]).*$" validation expression fulfills the above requirement.

  1. <table>  
  2. <tr>  
  3.     <td>  
  4.         <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
  5.         <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please Enter password"    
  6.     ControlToValidate="TextBox1"></asp:RequiredFieldValidator>  
  7.     <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="RegularExpressionValidator"  
  8.         ValidationExpression="^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!*@#$%^&+=]).*$"  
  9.         ControlToValidate="TextBox1"></asp:RegularExpressionValidator>  
  10.     </td>  
  11. </tr>  
  12. <tr>  
  13.     <td>  
  14.         <asp:Button ID="Button1" runat="server" Text="Button" />  
  15.     </td>  
  16. </tr>  
  17. </table> 

Using C# code:

One of our community guy (Vulpes) suggested this code to achieve the requirement…

If above any three conditions are satisfied then the creation of password is successful..

C# Code is:

  1. using System;     
  2. class Test    
  3. {    
  4.    static void Main()    
  5.    {    
  6.       string[] passWords = {"aX2#""sed2T""*v3X""Ae234&B""fg234""g1HL","#1$23""5a7%"};    
  7.       foreach(string passWord in passWords)    
  8.       {    
  9.          bool b = ValidatePassword(passWord);    
  10.          Console.WriteLine("'{0}' is{1} a valid password", passWord, b ? """n't");    
  11.       }    
  12.       Console.ReadKey();     
  13.    }    
  14.    static bool ValidatePassword(string passWord)    
  15.    {    
  16.       int validConditions = 0;     
  17.       foreach(char c in passWord)    
  18.       {    
  19.          if (c >= 'a' && c <= 'z')    
  20.          {    
  21.             validConditions++;    
  22.             break;    
  23.          }     
  24.       }     
  25.       foreach(char c in passWord)    
  26.       {    
  27.          if (c >= 'A' && c <= 'Z')    
  28.          {    
  29.             validConditions++;    
  30.             break;    
  31.          }     
  32.       }     
  33.       if (validConditions == 0) return false;     
  34.       foreach(char c in passWord)    
  35.       {    
  36.          if (c >= '0' && c <= '9')    
  37.          {    
  38.             validConditions++;    
  39.             break;    
  40.          }     
  41.       }     
  42.       if (validConditions == 1) return false;     
  43.       if(validConditions == 2)    
  44.       {    
  45.          char[] special = {'@''#''$''%''^''&''+''='}; // or whatever    
  46.          if (passWord.IndexOfAny(special) == -1) return false;    
  47.       }     
  48.       return true;    
  49.    }    
  50. }

 

That's it……Need any clarifications send me a message


Similar Articles