Password Validation with JavaScript
01/10/2021
Demo
Code
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Password Validation with JavaScript</title>
<link rel="stylesheet" type="text/css" href="style.css">
<link rel="stylesheet" type="text/css" href="https://demo.plantpot.works/assets/css/normalize.css">
<link href="https://use.fontawesome.com/releases/v5.6.1/css/all.css" rel="stylesheet">
<link rel="stylesheet" href="https://use.typekit.net/opg3wle.css">
<link rel="stylesheet" type="text/css" href="style.css">
<script src="password_validation.js"></script>
</head>
<body>
<div id="container">
<div class="form-box">
<form id="form" name="form" action="#" method="post">
<div class="form-input-box">
<label class="form-label" for="password">Password<span class="form-required"> *</span></label>
<div class="form-element">
<input id="password" class="form-input" type="password" name="password" placeholder="Enter password" oninput="password_validation()" required>
<label class="switch">
<input class="password-toggle" type="checkbox" name="checkbox" onchange="changeType()">
<span class="icon-eye">
<i class="fas fa-eye"></i>
<i class="fas fa-eye-slash"></i>
</span>
</label>
</div>
<div class="strength"><span class="percentage"></span></div>
<p id="msg" class="msg">Poor</p>
<ul class="password-policies">
<li class="lowercase">Lowercase</li>
<li class="uppercase">Uppercase</li>
<li class="numbers">Numbers</li>
<li class="symbols">Symbols</li>
<li class="length">At least 12 characters</li>
</ul>
</div>
</form>
</div>
</div>
</body>
</html>
CSS
@charset "utf-8";
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
html {
font-size: 16px;
}
body {
background-color: #f5f6e7;
font-family: futura-pt, sans-serif;
-webkit-tap-highlight-color: rgba(0,0,0,0);
}
ul {
list-style-type: none;
}
#container {
display: flex;
justify-content: center;
align-items: center;
position: relative;
width: 100%;
height: 100vh;
}
.form-box {
width: 100%;
max-width: 450px;
padding: 10px;
}
.form-label {
display: inline-block;
margin: .5rem 0;
color: #3d3935;
font-size: 1rem;
letter-spacing: .2px;
}
.form-required {
color: #bf0404;
}
.form-element {
position: relative;
}
.form-input {
width: 100%;
padding: 15px 10px;
border: 1px solid #23b08d;
border-radius: 4px;
background-color: #fff;
color: #3d3935;
font-size: 1rem;
outline: 0;
}
.form-input::placeholder {
color: #ccc;
font-size: 1rem;
}
.switch {
display: inline-block;
position: absolute;
top: 15px;
right: 10px;
background-color: #fff;
}
.form-element .password-toggle {
display: none;
}
.form-element .icon-eye {
cursor: pointer;
}
.form-element .password-toggle:not(:checked) + .icon-eye .fa-eye-slash {
display: none;
}
.form-element .password-toggle:checked + .icon-eye .fa-eye {
display: none;
}
.password-policies li {
padding: 5px 25px;
color: #ccc;
background-image: url(inactive.svg);
background-position: 0 center;
background-size: 20px 20px;
background-repeat: no-repeat;
}
.password-policies li.active {
color: #23b08d;
background-image: url(active.svg);
background-position: 0 center;
background-size: 20px 20px;
background-repeat: no-repeat;
}
.strength {
position: relative;
height: 8px;
margin: 10px 0 5px;
border-radius: 4px;
background-color: #fff;
transition: .5s;
}
.percentage {
position: absolute;
height: 8px;
border-radius: 4px;
background-color: #23b08d;
content: '';
}
.msg {
padding-right: 5px;
color: #3d3935;
font-size: 1rem;
text-align: right;
}
@media screen and (max-width: 480px) {
#container {
height: auto;
align-items: flex-start;
}
}
JavaScript
function changeType() {
var toggle = document.form.checkbox;
var password = document.getElementById("password");
if(toggle.checked) {
password.setAttribute("type", "text");
}
else {
password.setAttribute("type", "password");
}
}
function password_validation() {
var form = document.getElementById("form");
var password = document.getElementById("password").value;
var msg = document.getElementById("msg");
var patternLowercase = /[a-z]/;
var patternUppercase = /[A-Z]/;
var patternNumbers = /[\d]/;
var patternSymbols = /[!-/:-@[-`{-~]/;
var length = document.getElementsByClassName("length");
var lowercase = document.getElementsByClassName("lowercase");
var uppercase = document.getElementsByClassName("uppercase");
var numbers = document.getElementsByClassName("numbers");
var symbols = document.getElementsByClassName("symbols");
if(password.length >= 12) {
length[0].classList.add("active");
}
else {
length[0].classList.remove("active");
}
if(patternLowercase.test(password)) {
lowercase[0].classList.add("active");
}
else {
lowercase[0].classList.remove("active");
}
if(patternUppercase.test(password)) {
uppercase[0].classList.add("active");
}
else {
uppercase[0].classList.remove("active");
}
if(patternNumbers.test(password)) {
numbers[0].classList.add("active");
}
else {
numbers[0].classList.remove("active");
}
if(patternSymbols.test(password)) {
symbols[0].classList.add("active");
}
else {
symbols[0].classList.remove("active");
}
var active = document.getElementsByClassName("active");
var percentage = document.getElementsByClassName("percentage");
percentage[0].setAttribute("style", "width: " + active.length/5*100 + "%");
if(active.length <= 1) {
msg.innerHTML = "Poor";
}
else if(active.length == 2) {
msg.innerHTML = "Weak";
}
else if(active.length == 3) {
msg.innerHTML = "Midium";
}
else if(active.length == 4) {
msg.innerHTML = "Good";
}
else {
msg.innerHTML = "Strong";
}
}