ASP.NET - Password Strength Indicator using jQuery and XML

ASP.NET - Password Strength Indicator using jQuery and XML - Update 3 [See Below]


Last week, I had the opportunity to help implement and integrate a strong password policy to the legacy web application developed using ASP technology. The solution I proposed was to use jQuery to display the password strength meter to help users create strong password. Then one of my colleagues had asked "Do we have to modify the client-side script, code behind and password policy page if the client decided to alter the password policy?" The answer is "no", thanks to jQuery, the client-side script and code behind can share the same XML file. The password policy information is stored to an XML file and the client-side script and code behind are using the information in the XML file to perform the password strength validation.

I found several fine jQuery plug-in to display the user's password strength but I prefer to have something somewhat similar to the ASP.NET AJAX PasswordStrength control. After spending some time doing research, I was able to find all the necessary resources and assemble the jQuery plug-in to achieve the goal. Listed below are the features of the plug-in and I have put together a demo, everyone are welcome to download it.

  1. Display password strength indicator in text and progress bar meter.
  2. The password policy, bar color and width are stored in the XML file and consume by the client-side and server side
  3. Use XSLT to transform the password policy XML file into HTML.
Figure 1


Getting Started

Shown below is the content of the sample application. If you want to test the Classic ASP code, deploy the application to your IIS Web Server. The local ASP.NET Development Server does not support Classic ASP.

Figure 2


  • Default.aspx - sample code in ASP.NET c# with MasterPage
  • Default.aspx2 - sample code in ASP.NET c# without MasterPage
  • Password_strength_Indicator.asp - sample code in Classic ASP
  • jQuery_Password_Strength_Indicator.htm - sample code in HTML
  • PasswordPolicy.xml - contains password policy information
  • Password.xslt - to transform PasswordPoclicy.xml file into HTML format
  • jQuery.password-strength.js - this is the  plug-in I have created
  • Download latest jQuery library from here

The password strength indicator plug-in

In this section, I'll touch base briefly with the contents in the jQuery.password-strength.js file. The code is very straight forward and comments are included. There is a jQuery AJAX request to read the XML file and populate the local variables based on the data in the XML file. If you are unsure of the relative URL, I would recommend using absolute URL to your XML file. The getStrengthInfo() function contains the logic to check the password strength and return appropriate message based on user input. The password strength meter bar and text position are relative to the Textbox position.

Using the code

Include a Textbox control, jQuery library and the plug-in into the web page. Change the txtPassword id to your desire id. Use this line of code "var myPlugin = $("[id$='txtPassword']").password_strength();" to call the plug-in. To check if the password met the password policy, call the metReq() function with this line of code " myPlugin.metReq()". Please refer to Listing 1 for full details. The jQuery $("[id$='txtPassword']") selector will work with ASP.NET server control, so don't bother using my 'txtPassword.ClientID'. The bar color, width and password policy information can be modified through the XML file.

Listing 1

<div style="height:400px">
<br />
<asp:label runat="server" id="lblPassword" AssociatedControlId="txtPassword">Enter Password:</asp:label>
<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
<br />
<a id="passwordPolicy" href="#">Password policy</a> <br /><br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
<br /><br /><asp:Label ID="ResultLabel" runat="server" Text=""></asp:Label>
<script src="Script/jquery-1.4.4.min.js" type="text/javascript"></script>
<script src="Script/jquery.password-strength.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function() {
        var myPlugin = $("[id$='txtPassword']").password_strength();
        $("[id$='btnSubmit']").click(function() {
            return myPlugin.metReq(); //return true or false
        $("[id$='passwordPolicy']").click(function(event) {
            var width = 350, height = 300, left = (screen.width / 2) - (width / 2),
            top = (screen.height / 2) - (height / 2);
  "PasswordPolicy.xml", 'Password_poplicy', 'width=' + width + ',height=' + height + ',left=' + left + ',top=' + top);
            return false;

We can use the XLST to display the contents of the PasswordPolicy.xml or write code to extract its contents. I preferred to use XLST to avoid writing additional code. Displayed below is the password policy page. If you want to learn more on how to displaying XML with XSLT click on here.

Figure 3


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 2



Shown in listing 4 is the code to generate regular expression dynamically. So, tomorrow if your client told you to increase the required numeric 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 3

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!";

Question and Answer

Why the progress bar indicators look different from the one in the demo application?

Make sure that there is a proper DocType declare before the <html> tag. Click here to learn more about it.

Figure 4


How to change the TextBox to password mode?

For ASP.NET control, set the TextMode attribute to "Password".

For HTML control, set the type property to "Password".

Why I'm getting the error "This type of page is not served." when running the Classic ASP code on the local ASP.NET Development Server?

Deploy the demo application to the IIS Web Application Server.

Can I use this plug-in with other programming languages?


Display Different Colors in the Progress Bar

I have received several suggestions from the reader to have different colors in the progress bar meter. I tried to implement this with the least possible change to the existing code. The current implementation does not have a complex algorithm to assign different weights to the characters. Here's how I implemented it, please refer to listing 4 for full details.

1. The colors is based on the password length
2. If the password length between 0 and 33 percent, display red color
3. If the password length between 33 and 67 percent, display blue color
4. If above 67 percent, display the color specified in the PasswordPolicy.xml file

I also added useMultipleColors attribute to the PasswordPolixy.xml to give users the option to enable or disable the multiple colors.

Listing 5

 //use multiple colors
if (password_settings.useMultipleColors === "1") {
    //first 33% is red
    if (parseInt(strengthPercent) >= 0 && parseInt(strengthPercent) <= (barWidth * .33)) {
        backColor = "red";
    //33% to 66% is blue
    else if (parseInt(strengthPercent) >= (barWidth * .33) && parseInt(strengthPercent) <= (barWidth * .67)) {
        backColor = "blue";
    else {
        backColor = password_settings.barColor;

Figure 5
Password policy

ASP.NET - Password Strength Indicator using jQuery and XML โ€“ Update 3 (v01.03.00)

Recently a user reported to me the issue with the plug-in. The problem will occur (Figure 3.1) if the Label control ID contains the ID of the textbox control (Listing 3.1). 

Listing 3.1
<asp:label runat="server" id="lblPassword" AssociatedControlId="Password">Enter Password:</asp:label>
<asp:TextBox ID="Password" runat="server"></asp:TextBox>

Figure 3.1
This issue is very rare but they are several solution we can apply to suppress it.

1.    Exclude the id attribute from the Label control
2.    Make sure that the Label control ID does not contain the ID of the Textbox/input control
3.    Modify this line
var myPlugin = $("[id$='txtPassword']").password_strength();
var myPlugin = $("input[id$='txtPassword']").password_strength();


I hope someone will find this information useful and make your programming job easier. If you find any bugs or disagree with the contents or want to help improve this article, please drop me a line and I'll work with you to correct it. I would suggest downloading the demo and explore it in order to grasp the full concept of it because I might miss some important information in this article. Please send me an email if you want to help improve this article.



  • 01/09/2011 - First Release (v01.00.00)
  • 01/13/2011 - v01.01.00 Release
    -Set the width of the text container to the width of the meter bar + 40 pixel
    -Display missing lower case character correctly.
    -Include the source code for the plug-in.
  • 01/20/2011 - v01.02.00 Release
    - Added useMultipleColors attribute to PasswordPolicy.xml file. 1=Yes, 0=No
    - Added new logic in the plug-in to display background color in red if the strength is betwen 0 and 33%. The background color will be blue if the strength is between 33% and 67%.
  • 04/09/2012 - v01.03.00 - Addressed the issue with double indicator bars.


Dynamic Regular Expressions in Javascript
HTML <!DOCTYPE> Declaration
jQuery Password Indicator
Public functions from within a jQuery plug-in

Watch this script in action