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

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



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
        //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!";
            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
Title: sgwg   
Name: asdf
Date: 2012-08-30 11:06:21 PM
Title: asdf   
Name: asdf
Date: 2012-08-30 11:05:30 PM
Title: jashova   
Name: Esa
Date: 2011-02-14 6:27:36 AM
Good post
Title: Download and demo links   
Name: Bryian Tan
Date: 2011-02-01 7:37:32 PM

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
Download Sample

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

©Copyright 1998-2024  |  Page Processed at 2024-06-17 7:18:49 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search