Posted on March 10, 2021, 8 p.m. by Bishal ( 8091)
onkeyup
(one can also use JavaScript addEventListener keyup
or JavaScript onkeyup
) method to update the values of the input field.
let regExpWeak = /[a-z]/;
let regExpMedium = /\d+/;
let regExpStrong = /.[!,@,#,$,%,^,&,*,?,_,~,-,(,)]/;
Strength of the Password is Strong
Strength of the Password is Medium
Strength of the Password is Weak
index.html
index.html
<!DOCTYPE html>
<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<title>How to make Password Strength meter using JavaScript - Code With Bishal</title>
</head>
<!-- created by Code With Bishal - www.codewithbishal.com
-->
<body>
</body>
</html>
script.js
<head> </head>
of your HTML document:<script src="script.js"></script>
script.js
file:const input = document.querySelector("#password");
const weak = document.querySelector(".weak");
const medium = document.querySelector(".medium");
const strong = document.querySelector(".strong");
const btn = document.querySelector(".submit-btn");
btn.classList.add("disabled");
let regExpWeak = /[a-z]/;
let regExpMedium = /\d+/;
let regExpStrong = /.[!,@,#,$,%,^,&,*,?,_,~,-,(,)]/;
function checkPassword() {
//code here
}
style.css
<head> </head>
of your HTML document:<link href="style.css" rel="stylesheet">
style.css
file:.weak,.strong,.medium {
display: none;
}
input:focus {
box-shadow: none !important;
}
<!DOCTYPE html>
<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<title>How to make Password Strength meter using JavaScript - Code With Bishal</title>
</head>
<!-- created by Code With Bishal - www.codewithbishal.com
-->
<body>
<div class="container p-5 text-center">
<input onkeyup="checkPassword()" type="password" placeholder="Type password" class="form-control"
id="password">
<div class="icons">
<div class="weak alert alert-danger" role="alert">Weak Password</div>
<div class="medium alert alert-warning" role="alert">Medium Password</div>
<div class="strong alert alert-success" role="alert">Strong Password</div>
</div>
<a href="#" class="submit-btn btn btn-primary my-5">Submit</a>
</div>
</body>
</html>
.weak,.strong,.medium {
display: none;
}
input:focus {
box-shadow: none !important;
}
const input = document.querySelector("#password");
const weak = document.querySelector(".weak");
const medium = document.querySelector(".medium");
const strong = document.querySelector(".strong");
const btn = document.querySelector(".submit-btn");
btn.classList.add("disabled");
let regExpWeak = /[a-z]/;
let regExpMedium = /\d+/;
let regExpStrong = /.[!,@,#,$,%,^,&,*,?,_,~,-,(,)]/;
function checkPassword() {
if (input.value != "") {
if (input.value.length <= 3 && (input.value.match(regExpWeak) || input.value.match(regExpMedium) || input.value.match(regExpStrong))) no = 1;
if (input.value.length >= 6 && ((input.value.match(regExpWeak) && input.value.match(regExpMedium)) || (input.value.match(regExpMedium) && input.value.match(regExpStrong)) || (input.value.match(regExpWeak) && input.value.match(regExpStrong)))) no = 2;
if (input.value.length >= 6 && input.value.match(regExpWeak) && input.value.match(regExpMedium) && input.value.match(regExpStrong)) no = 3;
if (no == 1) {
input.style.borderColor = "#e74c3c";
input.style.background = "#fceae9";
weak.style.display = "block";
medium.style.display = "none";
strong.style.display = "none";
}
if (no == 2) {
input.style.borderColor = "#4df3cds";
input.style.background = "rgb(255, 243, 205, 0.5)";
weak.style.display = "none";
medium.style.display = "block";
strong.style.display = "none";
}
if (no == 3) {
input.style.borderColor = "#27ae60";
input.style.background = "#eafaf1";
strong.style.display = "block";
weak.style.display = "none";
medium.style.display = "none";
btn.classList.remove("disabled");
}
} else {
input.style.borderColor = "blue";
input.style.background = "white";
strong.style.display = "none";
weak.style.display = "none";
medium.style.display = "none";
btn.classList.add("disabled");
}
}