Implement Password complexity validation using JavaScript

Many times we want to show password complexity like (strong, weak or good). In this article, I will explain that how to implement password complexity show it using java script.
Source Code:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="complexity.aspx.cs" Inherits="complexity" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
  
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        $("#txtPassword").bind("blur", function () {
            if ($(this).val().length == 0) {
                $("#password_strength").html("");
                return;
            }
            var regex = new Array();
            regex.push("[A-Z]");
            regex.push("[a-z]");
            regex.push("[0-9]");
            regex.push("[$@$!%*#?&]");

            var passed = 0;

  
            for (var i = 0; i < regex.length; i++) {
                if (new RegExp(regex[i]).test($(this).val())) {
                    passed++;
                }
            }


            if (passed > 2 && $(this).val().length > 8) {
                passed++;
            }


            var color = "";
            var strength = "";
            switch (passed) {
                case 0:
                case 1:
                    strength = "Weak";
                    color = "red";
                    break;
                case 2:
                    strength = "Good";
                    color = "darkorange";
                    break;
                case 3:
                case 4:
                    strength = "Strong";
                    color = "green";
                    break;
                case 5:
                    strength = "Very Strong";
                    color = "darkgreen";
                    break;
            }
            $("#password_strength").html(strength);
            $("#password_strength").css("color", color);
        });
    });
</script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
     <input type="text" id="txtPassword" />
<span id="password_strength"></span>
    </div>
    </form>

</body>
</html>

Out-put:-

Implement Password complexity validation using JavaScript Implement Password complexity validation using JavaScript Reviewed by NEERAJ SRIVASTAVA on 12:11:00 AM Rating: 5

No comments:

Powered by Blogger.