ASP.NET - Password Strength Indicator using jQuery and XML
page 5 of 8
by Bryian Tan
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 42898/ 46

Code Behind or Server-Side Code

The regular expression that we are using is listed in listing 3. The numbers are adjustable and come from the XML file.

Listing 3

(?=^.{12,25}$)(?=(?:.*?\d){2})(?=.*[a-z])(?=(?:.*?[A-
Z]){2})(?=(?:.*?[!@#$%*()_+^&}{:;?.]){2})(?!.*\s)[0-9a-zA-Z!@#$%*()_+^&]*$

 

Shown in listing 4 is the code to generate regular expression dynamically. So, tomorrow if your client told you to increase the required digit in the password policy, you don't have to search or create a new regular expression. All you have to do is change the setting in the PasswordPolicy.xml file. You can verify the regular expression here.

Listing 4

void btnSubmit_Click(object sender, EventArgs e)
    {
        PasswordSetting passwordSetting = Helper.GetPasswordSetting();
        StringBuilder sbPasswordRegx = new StringBuilder(string.Empty);
 
        //min and max
        sbPasswordRegx.Append(@"(?=^.{" + passwordSetting.MinLength + "," +
            passwordSetting.MaxLength + "}$)");
 
        //numbers length
        sbPasswordRegx.Append(@"(?=(?:.*?\d){" + passwordSetting.NumsLength + "})");
 
        //a-z characters
        sbPasswordRegx.Append(@"(?=.*[a-z])");
 
        //A-Z length
        sbPasswordRegx.Append(@"(?=(?:.*?[A-Z]){" + passwordSetting.UpperLength + "})");
 
        //special characters length
        sbPasswordRegx.Append(@"(?=(?:.*?[" + passwordSetting.SpecialChars + "]){" + 
             passwordSetting.SpecialLength + "})");
 
        //(?!.*\s) - no spaces
        //[0-9a-zA-Z!@#$%*()_+^&] -- valid characters
        sbPasswordRegx.Append(@"(?!.*\s)[0-9a-zA-Z" + passwordSetting.SpecialChars + 
          "]*$");
 
        if (Regex.IsMatch(txtPassword.Text, sbPasswordRegx.ToString()))
        {
            ResultLabel.Text = "Password confront password policy!";
        }
        else
        {
            ResultLabel.Text = "Password does not confront password policy!";
        }
    }

 


View Entire Article

User Comments

Title: sdfsdf   
Name: sdfd
Date: 2012-08-31 10:21:04 AM
Comment:
sdfsfs
Title: sgwg   
Name: asdf
Date: 2012-08-30 11:06:21 PM
Comment:
asfsadffsadfasdfasf
Title: asdf   
Name: asdf
Date: 2012-08-30 11:05:30 PM
Comment:
asdfsf
Title: jashova   
Name: Esa
Date: 2011-02-14 6:27:36 AM
Comment:
Good post
Title: Download and demo links   
Name: Bryian Tan
Date: 2011-02-01 7:37:32 PM
Comment:
Hello,

I forgot to include the correct link to the download and demo in the original post. Here is the link.

Watch this script in action
http://download.ysatech.com/ASP-NET-jQuery-Password-Strength
Download Sample
http://download.ysatech.com/ASP-NET-jQuery-Password-Strength/Password_Strength_Indicator.zip






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-29 5:34:01 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search