Verifying password strength and adjusting color based on length of password:

I need assistance in creating a code that can determine the length of a password and change the color of an error message accordingly. For example, if the password is less than 4 characters, it should display in red; if 8 characters, yellow; and if 12 characters, green.

Here is my initial code:

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function checkPasswordLength(password){
     if (password.length > 12) {
  document.getElementById("errorSpan").innerHTML = "Limit is 12 characters";
     }
     else {
         document.getElementById("errorSpan").innerHTML = "";
     } 
}
</script>
</head>
<body>
<form>
<input type="password" name="pwd"onchange='checkPasswordLength(this.value)'>
<span id="errorSpan" style="color:red;"></span>
</div>
</form> 
</body>
</html>

Answer №1

Are you in search of a solution like this?

function validatePasswordStrength(password) {
  var errorSpan = document.getElementById("errorSpan");
  if (password.length >= 12) {
    errorSpan.style.color = "green";
    return;
  } else if (password.length >= 4) {
    errorSpan.style.color = "yellow";
    return;
  } else {
    errorSpan.style.color = "red";
  }
}
<input type="password" name="pwd" onkeyup='validatePasswordStrength(this.value)'>
<span id="errorSpan" style="color:red;">****</span>

You already utilized document.getElementById, so all that's left is to assign the desired color with .style.color. For text changes, .innerHTML can be used as before.

Please take note that I modified from using onchanged to onkeyup because onchanged only triggers when the control loses focus.

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

Extracting the JQuery library from its source code

Is there a simple method for extracting only the necessary functions from the jQuery library? I have found that many of the functions within the library are not being utilized, so it would be beneficial to remove them. ...

How can we organize and display the data from two linked arrays, one containing player names and the other containing

Looking for help on how to rank 3 players based on their scores using arrays. I've hit a roadblock and need guidance! Here's a brief example: Player 1 : Taylor Score Taylor : 15 Player 2 : Jordan Score Jordan : 20 Player 3 : Alex Score Alex : ...

I am experiencing an issue with the functionality of Handlebars registerPartial()

Check out my code snippet on JsFiddle Error Message: Uncaught Error - The partial social could not be found Note: I have made sure to include all necessary libraries. Looking forward to your assistance. Thank you! ...

Executing a pair of queries within the same table and integrating them within a unified route

How can I efficiently execute two queries on the same table in my project? I have been considering using promises, but my knowledge on them is limited. Even though I've researched about it, I am struggling to implement the correct structure. My main ...

Can you tell me the distinction between using RemoteWebDriver's executeScript() and Selenium's getEval() for executing

Can you explain the distinction between these two pieces of code: RemoteWebDriver driver = new FirefoxDriver(); Object result = driver.executeScript("somefunction();"); and this: RemoteWebDriver driver = new FirefoxDriver(); Selenium seleniumDriver = ne ...

Utilize specific CSS attributes from a class and apply them to a DOM element

It's clear that the question at hand is more complex than it initially appears. I'm not just looking for a way to apply a CSS class to a DOM element, as I'm already familiar with that (<div class="MyCssCLass"></div>) My goal is ...

Achieving a Stacked Image Effect Upon Clicking

I have a neat slideshow on my website showcasing 10 different images. My goal is to let users click on any image in the slideshow and have that specific image display below the slideshow without affecting its position. Instead of using a lightbox or moda ...

HTML and CSS for an off-canvas menu

Looking to create an off-canvas menu that smoothly pushes content out of view instead of cropping it. Additionally, I want to implement a feature that allows the menu to close when clicking outside of it. I found the code for the off-canvas menu on W3Scho ...

Reveal unseen information on the webpage upon clicking a link

I have successfully implemented a fixed header and footer on my webpage. The goal is to make it so that when a user clicks on a link in either the header or footer, the content of that page should appear dynamically. While I have explored various styles, ...

Invoking a Method in a React Component from its Parent Component

I am new to React and I have a question regarding how to call a method from a child component within the parent component. For example: var ChildClass = class Child { howDoICallThis () { console.log('Called!') } render () { retur ...

Is it feasible to maintain a persistent login session in Firebase without utilizing the firebase-admin package through the use of session cookies?

Currently, I am integrating Firebase into my next.js application for user login functionality. The issue I am facing is that users are getting logged out every time they switch paths within the site. Even though their session cookie has not expired, if the ...

JavaScript unable to access cookie on the initial attempt

I have been utilizing JavaScript to set and retrieve cookie values. The code I am using can be found at http://www.w3schools.com/js/js_cookies.asp. Upon page load, I check if the cookie exists or not. Everything is functioning properly except for the issue ...

What steps can you take to trigger redirection to a new window or tab on a mobile phone browser?

I've been working on a "Vue" application that serves as an order form. In the final step, when you opt to make a direct payment, you get redirected to a secure payment page which opens in a new browser tab. const url = "/api/es/orders/" + this. ...

Executing empty arguments with `execute_script` in Selenium with Firefox

I need help setting the value of a textarea using JavaScript instead of the send_keys() method. According to the documentation, I should be able to pass a webelement to execute_script as a parameter and refer to this parameter using the arguments array. H ...

When using $.getJSON, the callback function fails to execute

The Issue: The callback function in my $.getJSON request is not executing. When the page loads, nothing is displayed in the console or on the page. However, when the function is manually entered into the console, it works as expected. Here is the jQuery ...

Steps for establishing a thread pool for workers

I have been exploring the experimental functionality of the worker threads module in Node.js. After reading through the official docs and various articles (although limited in number), I decided to create a simple example. This example involves spawning te ...

Alter the color as the text moves beneath a see-through layer

Imagine a scenario where there is a page with text and on top of that, there is a transparent div that is smaller than the text area. Is it feasible to dynamically alter the color of the text that scrolls underneath the transparent div? Picture the trans ...

Display a Bootstrap Popover upon hovering over an alert message

I have created a custom notification window using the bootstrap alert class. The string has been trimmed for display, but I want the full message to appear in a popover when the user hovers over it. Below is the code snippet used for displaying notificatio ...

Add elements to an array following an AJAX request

On my .cshtml page, I have the following script: $(function () { var tempArray = []; var tbValue = $('#tb1').val(); $.ajax({ url: "/ControllerName/getdata", dataType: 'json', ...

Exploring the extraction of elements from list items using jQuery

Can you help me with removing the anchor tag from the list item? This is the current markup: <ul class="yith-wcan-list yith-wcan "> <li><a href="#">Item 1</a> <small class="count">8</small> <div class="c ...