SIGN UP MEMBER LOGIN:    
ARTICLE

Password Validator in C#

Posted by Jitendra Sampathirao Articles | ASP.NET Controls in C# September 09, 2011
Here I’m telling you about password Validator in C#.
Reader Level:

Everyone may experienced this of 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.



<table>
<tr>
    <td>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please Enter password" 
    ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="RegularExpressionValidator"
        ValidationExpression="^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!*@#$%^&+=]).*$"
        ControlToValidate="TextBox1"></asp:RegularExpressionValidator>
    </td>
</tr>
<tr>
    <td>
        <asp:Button ID="Button1" runat="server" Text="Button" />
    </td>
</tr>
</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:

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;

   }


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

Login to add your contents and source code to this article
share this article :
post comment
 

Thanks for share this article...This is very useful to me. This is C# Console application coding right? I am studying C# web application. how to write this program using C# web application?

Posted by Remya Mar 02, 2012

useful article to programmers for validating passwords in c#

Posted by muthu karthikeyan Sep 09, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor