Why is the checkmark still displaying red when it should be showing as green?

Are you struggling with password strength on your website? I faced a similar issue where the checkmarks remained red despite my attempts to turn them green.

If you want to change the checkmark color, here is some code that may help:

document.getElementById("password").addEventListener("input", function() {
  var password = this.value.trim();
  var capitalCheck = document.getElementById("capitalCheck").querySelector(".indicator");
  var numberCheck = document.getElementById("numberCheck").querySelector(".indicator");
  var symbolCheck = document.getElementById("symbolCheck").querySelector(".indicator");

  // Add logic to change colors here

});

You can customize the code snippet above to suit your needs. Good luck!

Answer №1

It appears that the valid attribute was overlooked in the span element, causing it to not be affected by the span.valid rule set for changing its color to green. I've made some adjustments to the code so that you no longer have to duplicate the regular expression for the same validation.

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>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The function of cookieParser() is causing confusion

Having an issue that I've been searching for answers to without success. When using app.use(express.cookieParser('Secret'));, how can we ensure that the 'Secret' is truly kept secret? I'm feeling a bit lost on this topic. Is ...

Is there a way to assign multiple ng-model values to individual input fields generated by a single directive?

After developing a custom directive to dynamically include various form fields, I encountered an issue where the ng-model gets duplicated when more than one directive-based field is added. As someone new to working with directives, any assistance would be ...

CSS: Problem Arising from Line Connections Between Tree Elements

I'm currently working on connecting tree nodes with lines in a drawing. I've managed to achieve it to some extent, but there are a few issues like dangling lines that I need to fix. You can find the implementation on codepen here: https://codepe ...

Can @keyframes be iterated through CSS (particularly Stylus)?

Here is some Stylus code I'm working with: for i in (1..3) .l:nth-child({i}) opacity (i / 5) When executed, the output looks like this: .l:nth-child(1) { opacity: 0.2; } .l:nth-child(2) { opacity: 0.4; } .l:nth-child(3) { opacity: 0.6; } ...

Creating a website without access to the internet

When I'm working on my laptop in locations without internet access, I need to build a website. How can I assess the progress of my pages without an active browser? ...

When padding is added on an MVC 5 page, the Bootstrap 4.1.1 class option for rounded-circle becomes distorted

Incorporating VS 2017 and bootstrap 4.1.1 into an MVC 5 page, I am facing a challenge in adding right-side padding to an image without distorting the circular shape of the image, as shown below. When I apply the padding, the circle becomes warped, but remo ...

Is it possible for an absolutely positioned element to have floating elements wrapping around it?

Within my content box (#left-home-content), I have a series of paragraphs followed by a link in a <span> tag (.read-more-link), and then a <div> tag (#left-home-spread) that needs to be positioned absolutely at the bottom of the box. This may s ...

Submitting jQuery Ajax forms multiple times using the POST method

After trying various solutions for this issue, none seem to effectively address my problem. Here are some examples: $("form#sending-notice-form").unbind('submit').bind('submit', function (e) { e.preventDefault(); .. }); While ...

I am experiencing an issue where my ajax code is unable to display the

After spending countless hours trying to make this work, I find myself stuck. I've set up a simple guestbook page and have created this code to generate a JSON file: <?php /* Constants for database settings */ define("DBHOST", "localhost"); defin ...

Express get requests are failing to handle query strings

Whenever I try to extract the year and month from the URL like this: http://localhost:3000/api/posts/2018/4, the code doesn't seem to work properly. Instead, it returns an error saying: Cannot GET /api/posts/2018/4 Here's the JavaScript code I&a ...

Issue with cancelling a file upload is not functioning properly on Internet Explorer

My experience with file uploads in Internet Explorer is causing some issues. While other browsers work smoothly when canceling file uploads, Internet Explorer seems to have a different behavior. In other browsers, clicking the "Cancel" button during a fil ...

The split function is creating redundant values

Please help me with this issue I am facing while trying to split a string using split(). It's causing duplicate values which is frustrating. Can someone please assist me? The string I need to split is a comma-separated response from a database, and h ...

`Implementing Typescript code with Relay (Importing with System.js)`

Is there a way to resolve the error by including system.js or are there alternative solutions available? I recently downloaded the relay-starter-kit (https://github.com/relayjs/relay-starter-kit) and made changes to database.js, converting it into databas ...

Converting a Base64 URL to an image object: A step-by-step guide

I currently have a base64 URL in the following format: data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAA My objective is to convert this into an image file with the following properties: [File] 0: File lastModified: 1559126658701 lastModifiedDate: Wed M ...

Redirect link depending on JSON callback information

I experimented with utilizing JavaScript to automatically redirect website visitors based on their country. The code snippet below demonstrates how the visitor's IP is checked to determine their country, such as CN for China, and then redirects them ...

Creating various styles for cards using ngFor within Angular

I have been working on applying different styles to cards created using ngFor in Angular. Here's how it currently looks: https://i.sstatic.net/UhbSp.png My aim is to set unique styles for each card. Due to the ngFor loop used in creating them, I ini ...

Optimal method for arranging Vue components

When deciding where to position a child element, should it be done within its own CSS scope or from the parent? In the scenario provided below, would it be more effective to use margin-left: auto; on the parent element or the child element (both options a ...

The use of functions and parentheses in Javascript

I'm having trouble understanding how the f() functions work. Could someone please explain why it prints two '1's? I know it prints '1' for every '()' after f(f), but I don't understand the reasoning behind it. fun ...

E/AndroidRuntime: CRITICAL ERROR: main --- java code in an android app

I've been working on a map application and I've run into some issues. Can anyone help me figure out what's wrong with my code? ERROR 10-25 01:37:41.296 28673-28673/com.onemap.activities E/AndroidRuntime: FATAL EXCEPTION: main 10-25 01:37:4 ...

Steps for obtaining images using lazy loading: <img loading="lazy"

After successfully utilizing the JavaScript-executer to locate the ImageElement, I encountered an error when attempting to extract the URL for downloading the image.svg. The error message reads "NoSuchElementException." Below is a snippet of my code: ((J ...