Click here to Skip to main content
12,758,605 members (39,231 online)
Click here to Skip to main content
Add your own
alternative version


185 bookmarked
Posted 11 Jan 2011

ASP.NET - Password Strength Indicator using jQuery and XML

, 31 Jul 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
ASP.NET Password Strength Indicator somewhat similar to AJAX PasswordStrength extender control behavior and implemented by using jQuery and XML.

Table of Contents


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 is 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 consumed by the client-side and server side
  3. Use XSLT to transform the password policy XML file into HTML.
Figure 1

Password Strength steps

Figure 2

Password policy

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 3

Project Structure

  • 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 the 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. Shown below is the code in the jQuery.password-strength.js file.

Listing 1
(function($) {
    var password_Strength = new function() {

        //return count that match the regular expression
        this.countRegExp = function(passwordVal, regx) {
            var match = passwordVal.match(regx);
            return match ? match.length : 0;

        this.getStrengthInfo = function(passwordVal) {
            var len = passwordVal.length;
            var pStrength = 0; //password strength
            var msg = "", inValidChars = ""; //message

            //get special characters from xml file
            var allowableSpecilaChars = new RegExp
		("[" + password_settings.specialChars + "]", "g")

            var nums = this.countRegExp(passwordVal, /\d/g), //numbers
		lowers = this.countRegExp(passwordVal, /[a-z]/g),
		uppers = this.countRegExp(passwordVal, /[A-Z]/g), //upper case
		specials = this.countRegExp(passwordVal,
				allowableSpecilaChars), //special characters
		spaces = this.countRegExp(passwordVal, /\s/g);

            //check for invalid characters
            inValidChars = passwordVal.replace(/[a-z]/gi, "") +
				inValidChars.replace(/\d/g, "");
            inValidChars = inValidChars.replace(/\d/g, "");
            inValidChars = inValidChars.replace(allowableSpecilaChars, "");

            //check space
            if (spaces > 0) {
                return "No spaces!";

            //invalid characters
            if (inValidChars !== '') {
                return "Invalid character: " + inValidChars;

            //max length
            if (len > password_settings.maxLength) {
                return "Password too long!";

            //GET NUMBER OF CHARACTERS left
            if ((specials + uppers + nums + lowers) < password_settings.minLength) {
                msg += password_settings.minLength -
		(specials + uppers + nums + lowers) + " more characters, ";

            //at the "at least" at the front
            if (specials == 0 || uppers == 0 || nums == 0 || lowers == 0) {
                msg += "At least ";

            //GET NUMBERS
            if (nums >= password_settings.numberLength) {
                nums = password_settings.numberLength;
            else {
                msg += (password_settings.numberLength - nums) + " more numbers, ";

            //special characters
            if (specials >= password_settings.specialLength) {
                specials = password_settings.specialLength
            else {
                msg += (password_settings.specialLength - specials) + " more symbol, ";

            //upper case letter
            if (uppers >= password_settings.upperLength) {
                uppers = password_settings.upperLength
            else {
                msg += (password_settings.upperLength - uppers) +
					" Upper case characters, ";

            //strength for length
            if ((len - (uppers + specials + nums)) >=
		(password_settings.minLength - password_settings.numberLength -
		password_settings.specialLength - password_settings.upperLength)) {
                pStrength += (password_settings.minLength -
		password_settings.numberLength - password_settings.specialLength -
            else {
                pStrength += (len - (uppers + specials + nums));

            //password strength
            pStrength += uppers + specials + nums;

            //detect missing lower case character
            if (lowers === 0) {
                if (pStrength > 1) {
                    pStrength -= 1; //Reduce 1
                msg += "1 lower case character, ";

            //strong password
            if (pStrength == password_settings.minLength && lowers > 0) {
                msg = "Strong password!";

            return msg + ';' + pStrength;

    //default setting
    var password_settings = {
        minLength: 12,
        maxLength: 25,
        specialLength: 1,
        upperLength: 1,
        numberLength: 1,
        barWidth: 200,
        barColor: 'Red',
        specialChars: '!@#$', //allowable special characters
        metRequirement: false,
        useMultipleColors: 0

    //password strength plugin
    $.fn.password_strength = function(options) {

        //check if password met requirement
        this.metReq = function() {
            return password_settings.metRequirement;

        //read password setting from xml file
            type: "GET",
            url: "PasswordPolicy.xml", //use absolute link if possible
            dataType: "xml",
            success: function(xml) {

                $(xml).find('Password').each(function() {
                    var _minLength = $(this).find('minLength').text(),
                    _maxLength = $(this).find('maxLength').text(),
                    _numsLength = $(this).find('numsLength').text(),
                    _upperLength = $(this).find('upperLength').text(),
                    _specialLength = $(this).find('specialLength').text(),
                    _barWidth = $(this).find('barWidth').text(),
                    _barColor = $(this).find('barColor').text(),
                    _specialChars = $(this).find('specialChars').text(),
                    _useMultipleColors = $(this).find('useMultipleColors').text();

                    //set variables
                    password_settings.minLength = parseInt(_minLength);
                    password_settings.maxLength = parseInt(_maxLength);
                    password_settings.specialLength = parseInt(_specialLength);
                    password_settings.upperLength = parseInt(_upperLength);
                    password_settings.numberLength = parseInt(_numsLength);
                    password_settings.barWidth = parseInt(_barWidth);
                    password_settings.barColor = _barColor;
                    password_settings.specialChars = _specialChars;
                    password_settings.useMultipleColors = _useMultipleColors;

        return this.each(function() {

            //bar position
            var barLeftPos = $("[id$='" + + "']").position().left +
				$("[id$='" + + "']").width();
            var barTopPos = $("[id$='" + + "']").position().top +
				$("[id$='" + + "']").height();

            //password indicator text container
            var container = $('<span></span>')
            .css({ position: 'absolute', top: barTopPos - 6,
		left: barLeftPos + 15, 'font-size': '75%',
		display: 'inline-block', width: password_settings.barWidth + 40 });

            //add the container next to textbox

            //bar border and indicator div
            var passIndi = $('<div id="PasswordStrengthBorder"></div>
		<div id="PasswordStrengthBar" class="BarIndicator"></div>')
            .css({ position: 'absolute', display: 'none' })
            .eq(0).css({ height: 3, top: barTopPos - 16, left: barLeftPos + 15,
		'border-style': 'solid', 'border-width': 1, padding: 2 }).end()
            .eq(1).css({ height: 5, top: barTopPos - 14, left: barLeftPos + 17 }).end()

            //set max length of textbox
            //$("[id$='" + + "']").attr('maxLength', password_settings.maxLength);

            //add the border and div

            $(this).keyup(function() {

                var passwordVal = $(this).val(); //get textbox value

                //set met requirement to false
                password_settings.metRequirement = false;

                if (passwordVal.length > 0) {

                    var msgNstrength = password_Strength.getStrengthInfo(passwordVal);

                    var msgNstrength_array = msgNstrength.split(";"), strengthPercent = 0,
                    barWidth = password_settings.barWidth,
			backColor = password_settings.barColor;

                    //calculate the bar indicator length
                    if (msgNstrength_array.length > 1) {
                        strengthPercent = (msgNstrength_array[1] /
				password_settings.minLength) * barWidth;

			({ display: 'inline', width: barWidth });

                    //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;

                    $("[id$='PasswordStrengthBar']").css({ display: 'inline',
			width: strengthPercent, 'background-color': backColor });

                    //remove last "," character
                    if (msgNstrength_array[0].lastIndexOf(",") !== -1) {
				(0, msgNstrength_array[0].length - 2));
                    else {

                    if (strengthPercent == barWidth) {
                        password_settings.metRequirement = true;
                else {
                    $("[id$='PasswordStrengthBorder']").css("display", "none"); //hide
                    $("[id$='PasswordStrengthBar']").css("display", "none"); //hide

Using the Code

Include a Textbox control, jQuery library and the plug-in into the web page. Change the txtPassword id to your desired 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 2 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 2
<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

<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 display XML with XSLT, click here.

Figure 4

Password policy

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

Question and Answer

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

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

Figure 5

Password policy

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 Progressbar

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 5 for full details.

  • The colors are based on the password length.
  • If the password length is between 0 and 33 percent, display red color.
  • If the password length is between 33 and 67 percent, display blue color.
  • 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;

Duplicate password strength indicator issue

Recently a user reported to me the issue with the Password Strength Indicator using jQuery and XML 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

double bars

Solution: 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:
  4. var myPlugin = $("[id$='txtPassword']").password_strength();


    var myPlugin = $("input[id$='txtPassword']").password_strength();

Duplicate password strength indicator issue: Update #2

Couple weeks ago a CodeProject member reported that the "Password Strength Indicator using jQuery and XML plug-in" is displaying duplicate indicators. Yesterday, I was trying to integrate the plug-in with the MVC 3 application and ran into the mentioned issue.

Figure 4.1

After spending some time digging into it, I noticed that there was a problem with the jQuery ID selectors. I should use the Attribute Equals Selector [id="value"] instead of Attribute Ends with Selector [id$="value"]. The former selector selects elements that have the specified attribute with a value exactly equal to a certain value. The later selector selects elements that have the specified attribute with a value ending exactly with a given string. That explain why the bar indicator appears next to both the password and confirm password textboxes. I have updated the plug-in and here is the correct approach to use the plug-in.

Listing 4.1
<script type="text/javascript">
    $(document).ready(function () {
        var myPlugin = $("input[id='Password']").password_strength();

        $("[id='submit']").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;


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 between 0 and 33%. The background color will be blue if the strength is between 33% and 67%.
  • 07/23/2012 - v01.03.00 release



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Bryian Tan
Software Developer (Senior)
United States United States
I have over three years of experience working with Microsoft technologies. I have earned my Microsoft Certified Technology Specialist (MCTS) certification. I'm a highly motivated self-starter with an aptitude for learning new skills quickly.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Abinash Bishoyi24-Jul-12 4:11
memberAbinash Bishoyi24-Jul-12 4:11 
GeneralRe: My vote of 5 Pin
Bryian Tan8-Aug-12 17:14
memberBryian Tan8-Aug-12 17:14 
GeneralMy vote of 5 Pin
AlluvialDeposit24-Jul-12 0:34
memberAlluvialDeposit24-Jul-12 0:34 
GeneralRe: My vote of 5 Pin
Bryian Tan24-Jul-12 14:15
memberBryian Tan24-Jul-12 14:15 
QuestionYou deserve a 5, and I'm giving it! Pin
Dewey23-Jul-12 22:08
memberDewey23-Jul-12 22:08 
GeneralRe: You deserve a 5, and I'm giving it! Pin
Bryian Tan24-Jul-12 14:15
memberBryian Tan24-Jul-12 14:15 
GeneralRe: You deserve a 5, and I'm giving it! Pin
Bryian Tan9-Aug-12 16:15
memberBryian Tan9-Aug-12 16:15 
GeneralRegarding Duplicate Indicators Pin
erhardeep7-Jul-12 1:31
membererhardeep7-Jul-12 1:31 

First of all thanks for this nice post. I want to discuss about bug on indicator. Suppose in html page there are two password fields. Suppose I apply your plugin on both fields. When I try to enter the password in the first field automatically the indicator show on another field and vice versa.

Please fix this as soon as possible. Fortunately I found the solution for this bug.

Hardeep Singh
GeneralRe: Regarding Duplicate Indicators Pin
Bryian Tan23-Jul-12 20:11
memberBryian Tan23-Jul-12 20:11 
GeneralMy vote of 3 Pin
ghousebasha16-Apr-12 21:33
memberghousebasha16-Apr-12 21:33 
GeneralRe: My vote of 3 Pin
Bryian Tan24-Jul-12 14:15
memberBryian Tan24-Jul-12 14:15 
GeneralMy vote of 5 Pin
chetan virkar12-Mar-12 2:15
memberchetan virkar12-Mar-12 2:15 
GeneralRe: My vote of 5 Pin
Bryian Tan12-Apr-12 8:17
memberBryian Tan12-Apr-12 8:17 
Generalmy vote of 5 Pin
Uday P.Singh23-Feb-12 7:51
memberUday P.Singh23-Feb-12 7:51 
GeneralRe: my vote of 5 Pin
Bryian Tan12-Apr-12 8:17
memberBryian Tan12-Apr-12 8:17 
GeneralVote 5 Pin
shaheen_mix17-Dec-11 0:10
membershaheen_mix17-Dec-11 0:10 
GeneralRe: Vote 5 Pin
Bryian Tan29-Jan-12 16:10
memberBryian Tan29-Jan-12 16:10 
GeneralMy vote of 5 Pin
Reiss11-Nov-11 0:15
memberReiss11-Nov-11 0:15 
GeneralRe: My vote of 5 Pin
Bryian Tan29-Jan-12 16:10
memberBryian Tan29-Jan-12 16:10 
GeneralMy vote of 5 Pin
eng \ ahmed farag4-Jul-11 4:54
membereng \ ahmed farag4-Jul-11 4:54 
GeneralRe: My vote of 5 Pin
Bryian Tan6-Jul-11 4:28
memberBryian Tan6-Jul-11 4:28 
GeneralMy vote of 4 Pin
Reelix8-Mar-11 21:59
memberReelix8-Mar-11 21:59 
GeneralRe: My vote of 4 Pin
TG_Cid28-Feb-12 13:29
memberTG_Cid28-Feb-12 13:29 
GeneralRe: My vote of 4 Pin
amitgajjar9-Apr-12 20:22
memberamitgajjar9-Apr-12 20:22 
GeneralRe: My vote of 4 Pin
TG_Cid11-Apr-12 19:39
memberTG_Cid11-Apr-12 19:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 31 Jul 2012
Article Copyright 2011 by Bryian Tan
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid