document.getElementById("password").addEventListener("input", function() {
var password = this.value.trim(); // Trim leading and trailing spaces
var capitalCheck = document.getElementById("capitalCheck").querySelector(".indicator");
var numberCheck = document.getElementById("numberCheck").querySelector(".indicator");
var symbolCheck = document.getElementById("symbolCheck").querySelector(".indicator");
checkPassword(password, capitalCheck, /[A-Z]/);
checkPassword(password, numberCheck, /\d/);
checkPassword(password, symbolCheck, /[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]/);
});
function checkPassword(password, el, regex) {
if (regex.test(password)) {
el.textContent = '✔';
el.classList.add('valid');
} else {
el.textContent = '✘';
el.classList.remove('valid');
}
el.style.visibility = 'visible'; // I'm unsure about this line, but I've left it as is per your preferences
}
function calculateTime() {
var password = document.getElementById("password").value;
var charCount = password.length;
var hasUpperCase = /[A-Z]/.test(password);
var hasNumber = /\d/.test(password);
var hasSymbol = /[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]/.test(password);
var possibleCharacters = 26 + (hasUpperCase ? 26 : 0) + (hasNumber ? 10 : 0) + (hasSymbol ? 32 : 0);
var permutations = Math.pow(possibleCharacters, charCount);
var seconds = permutations / 1000000000;
var minutes = seconds / 60;
var hours = minutes / 60;
var days = hours / 24;
var years = days / 365;
var result = "Approximately ";
if (years >= 1) {
result += Math.floor(years) + " years, " + Math.floor(days % 365) + " days, " + Math.floor(hours % 24) + " hours, " + Math.floor(minutes % 60) + " minutes, and " + Math.floor(seconds % 60) + " seconds needed to crack this password.";
} else if (days >= 1) {
result += Math.floor(days) + " days, " + Math.floor(hours % 24) + " hours, " + Math.floor(minutes % 60) + " minutes, and " + Math.floor(seconds % 60) + " seconds needed to crack this password.";
} else if (hours >= 1) {
result += Math.floor(hours) + " hours, " + Math.floor(minutes % 60) + " minutes, and " + Math.floor(seconds % 60) + " seconds needed to crack this password.";
} else if (minutes >= 1) {
result += Math.floor(minutes) + " minutes, and " + Math.floor(seconds % 60) + " seconds needed to crack this password.";
} else {
result += Math.ceil(seconds) + " seconds needed to crack this password.";
}
document.getElementById("result").innerText = result;
}
.info-container {
background-color: #f0f0f0;
padding: 20px;
margin-bottom: 20px;
border: 1px solid #ccc;
border-radius: 5px;
text-align: center;
}
.container {
background-color: #fff;
padding: 30px;
width: 400px;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
.criteria-item {
margin-bottom: 10px;
}
body {
font-family: Arial, sans-serif;
background-color: #f5f5f5;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: flex-start;
}
.container>h1 {
font-size: 24px;
text-align: center;
margin-bottom: 20px;
}
label {
font-size: 16px;
display: block;
margin-bottom: 10px;
}
input[type="password"] {
width: 100%;
padding: 10px;
margin-bottom: 20px;
border: 1px solid #ccc;
border-radius: 3px;
}
button {
padding: 10px 20px;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 3px;
cursor: pointer;
font-size: 16px;
transition: background-color 0.3s ease;
}
button:hover {
background-color: #0056b3;
}
p#result {
margin-top: 20px;
font-size: 18px;
text-align: center;
}
ul.criteria {
display: flex;
justify-content: space-between;
margin-bottom: 20px;
}
ul.criteria>li {
font-size: 16px;
}
span.indicator {
font-size: 16px;
color: red;
}
span.valid {
color: green;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Password Strength Checker</title>
</head>
<body>
<br> <br> <br>
<div class="info-container">
<p>Insert text here...</p>
</div>
<div class="container">
<h1>Password Strength Checker</h1>
<label for="password">Enter Password:</label>
<input type="password" id="password" placeholder="Password">
<div class="criteria">
<div class="criteria-item" id="capitalCheck">Capital Letters: <span class="indicator"></span></div>
<div class="criteria-item" id="numberCheck">Numbers: <span class="indicator"></span></div>
<div class="criteria-item" id="symbolCheck">Symbols: <span class="indicator"></span></div>
</div>
<button onclick="calculateTime()">Calculate</button>
<div id="result"></div>
</div>