You are currently viewing Password Strength Checker In CSS & JavaScript

Password Strength Checker In CSS & JavaScript

Password Strength Checker In CSS & JavaScript

Hello everyone. Welcome to today’s tutorial on Codewithrandom. We’ll learn how to make a Password Strength Checker which will tell the strength of the password to the user so that he can change it and his confidential details are secured. In Today’s session, We will use HTML, CSS, and JavaScript to complete this Password Strength Project.

The HTML (Hypertext Markup Language) will help us to create the structure for the list with some necessary attributes and elements to make Password Strength Project.

Then we will use CSS (Cascading Stylesheet) which will help us to style or design the project with suitable padding and alignment in the Password Strength Project.

At last, we will use JS (JavaScript) which will add logic to make the Password Strength Project responsive from the user end.

Using CSS we present Password Strength Checker In CSS & JavaScript projects with source code available for you to copy and paste directly into your own project.

I hope you have got an idea about the project.

HTML Code for Password Strength Checker

First, we’ll start with creating the structure of the Password Strength project for that as you can see in the above code we have used all the necessary elements & attributes to set up the structure. Let us know code the CSS part to add styling and aligned the tags.

<html lang="en">
<head>
    <title>Password Strength Checker</title>
    <!-- FontAwesome Icons -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
    <!-- Google Font -->
    <link rel="preconnect" href="https://fonts.gstatic.com">
    <link href="https://fonts.googleapis.com/css2?family=Roboto+Mono&display=swap" rel="stylesheet">
    <!--Stylesheet-->
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="wrapper">
        <div class="container">
            <input type="password" id="password" oninput="strengthChecker()">
            <span id="toggle" onclick="toggle()">
                <i class="fas fa-eye"></i>
            </span>
            <div id="strength-bar"></div>
        </div>
        <p id="msg"></p>
    </div>
    <!--Script-->
    <script src="script.js"></script>
</body>
</html>

Notification bell javascript | Create notification bell using Html CSS Javascript

CSS Code for Password Strength Checker

Second, comes the CSS code which we have styled for the structure we have padded as well as aligned the Password Strength project so that it is properly situated and doesn’t get messy with suitable CSS elements. Now, let’s code the JavaScript part to make it responsive.

*,
*:before,
*:after{
    padding: 0;
    margin: 0;
    box-sizing: border-box;
}
body{
    height: 100vh;
    background: linear-gradient(
        135deg,
        #61d954,
        #2ebf75
    );
}
.wrapper{
    background-color: #ffffff;
    width: 450px;
    padding: 50px 0;
    position: absolute;
    transform: translate(-50%,-50%);
    left: 50%;
    top: 50%;
    border-radius: 8px;
    box-shadow: 0 20px 25px rgba(0,0,0,0.2);
}
.container{
    width: 300px;
    position: relative;
    margin: auto;
}
input,
p{
    font-family: "Roboto Mono", monospace;
}
input{
    width: 100%;
    height: 50px;
    padding: 0 40px 0 20px;
    position: relative;
    background-color: #f5f5f5;
    border: none;
    outline: none;
    border-radius: 5px 5px 0 0;
}

#toggle{
    position: absolute;
    top: 17px;
    right: 15px;
    color: #808080;
    cursor: pointer;
}
.strength{
    width: 25%;
    display: inline-block;
    position: relative;
    height: 100%;
    bottom: 5px;
}
#strength-bar{
    background-color: #dcdcdc;
    height: 10px;
    position: relative;
}
p{
    width: 100%;
    text-align: center;
    margin-top: 20px;
}

Building A Dashboard Design With FlexBox Html CSS

JavaScript Code for Password Strength Checker

Last stage of the project the JavaScript which we added the logic and coded as per the requirement with some conditions. Let us see the Final Output of the project Password Strength Checker using  CSS & JavaScript.

let parameters = {
    count : false,
    letters : false,
    numbers : false,
    special : false
}
let strengthBar = document.getElementById("strength-bar");
let msg = document.getElementById("msg");

function strengthChecker(){
    let password = document.getElementById("password").value;

    parameters.letters = (/[A-Za-z]+/.test(password))?true:false;
    parameters.numbers = (/[0-9]+/.test(password))?true:false;
    parameters.special = (/[!\"$%&/()=?@~`\\.\';:+=^*_-]+/.test(password))?true:false;
    parameters.count = (password.length > 7)?true:false;

    let barLength = Object.values(parameters).filter(value=>value);

    console.log(Object.values(parameters), barLength);

    strengthBar.innerHTML = "";
    for( let i in barLength){
        let span = document.createElement("span");
        span.classList.add("strength");
        strengthBar.appendChild(span);
    }

    let spanRef = document.getElementsByClassName("strength");
    for( let i = 0; i < spanRef.length; i++){
        switch(spanRef.length - 1){
            case 0 :
                spanRef[i].style.background = "#ff3e36";
                msg.textContent = "Your password is very weak";
                break;
            case 1:
                spanRef[i].style.background = "#ff691f";
                msg.textContent = "Your password is weak";
                break;
            case 2:
                spanRef[i].style.background = "#ffda36";
                msg.textContent = "Your password is good";
                break;
            case 3:
                spanRef[i].style.background = "#0be881";
                msg.textContent = "Your password is strong";
                break;
        }
    }
}


function toggle(){
    let password = document.getElementById("password");
    let eye = document.getElementById("toggle");

    if(password.getAttribute("type") == "password"){
        password.setAttribute("type","text");
        eye.style.color = "#0be881";
    }
    else{
        password.setAttribute("type","password");
        eye.style.color = "#808080";
    }
}

Final Output


We have successfully created our Password Strength Checker using HTML, CSS & JavaScript. You can use this project for your personal needs and the respective lines of code are given with the code pen link mentioned above.

If you find out this Blog helpful, then make sure to search code with random on google for Front End Projects with Source codes and make sure to Follow the Code with Random Instagram page.

Thank You For Visiting!!!

Code Idea – codingartist

Written By – Harsh Sawant

Code By – @harshh9

Leave a Reply