This is the challenge below:
Write an algorithm to check the validity of a password inputed by a user with the criteria below.
If these criteria at met, the user is returned a percentage value of how strong his/her password is:
●At least 1 letter between lowercase [ a - z ] -> 25%
●At least 1 letter between uppercase [ A - Z ] -> 25%
●At least 1 number between [ 0 - 9 ] -> 25%
●At least 1 character from [ $@#&! ] -> 25%
If these criteria are met, the user is sent a message according to condition met:
●Minimum length of password is 6 (display a message)
●Maximum length of password is 12
For Html:
<body>
<form class="center-block">
<input type="text" id="password" autocomplete="off" class="form-control input-lg">
<progress max="100" value="0" id="meter"></progress>
<button class="btn btn-success btn-lg btn-block">Show Password Strength</button>
</form>
<div class="textbox text-center"> password </div>
</body>
For JavaScript:
var code
=document.getElementById("password");
var strengthbar= document.getElementById("meter");
code.addEventListener("keyup", function(){checkpassword(code.value)
})
var display =document.getElementsByClassName("textbox")[0];
function checkpassword(password)
{
var strength=0;
if (password.match(/[a-z]+/)){
strength+=1;
}
if (password.match(/[A-Z]+/)){
strength+=1;
}
if (password.match(/[0-9]+/)){
strength+=1;
}
if (password.match(/[$@#&!]+/)){
strength+=1;
}
if (password.length<6){
display.innerHTML="minimum number of characters is 6":
}
if (password.length>12){
display.innerHTML="maximum number of characters is 12";
}
switch(strength){
case 0:
strengthbar.value=0;
break;
case 1:
strengthbar.value=25;
break;
case 2:
strengthbar.value=50;
break;
case 3:
strengthbar.value=75;
break;
case 4:
strengthbar.value=100;
break; }
}
Above is my own solution to the problem. It isn't giving me any errors but the JavaScript is not generating any results on my html.
Here is a link to the pen I'm using
This is the challenge below:
Write an algorithm to check the validity of a password inputed by a user with the criteria below.
If these criteria at met, the user is returned a percentage value of how strong his/her password is:
●At least 1 letter between lowercase [ a - z ] -> 25%
●At least 1 letter between uppercase [ A - Z ] -> 25%
●At least 1 number between [ 0 - 9 ] -> 25%
●At least 1 character from [ $@#&! ] -> 25%
If these criteria are met, the user is sent a message according to condition met:
●Minimum length of password is 6 (display a message)
●Maximum length of password is 12
For Html:
<body>
<form class="center-block">
<input type="text" id="password" autocomplete="off" class="form-control input-lg">
<progress max="100" value="0" id="meter"></progress>
<button class="btn btn-success btn-lg btn-block">Show Password Strength</button>
</form>
<div class="textbox text-center"> password </div>
</body>
For JavaScript:
var code
=document.getElementById("password");
var strengthbar= document.getElementById("meter");
code.addEventListener("keyup", function(){checkpassword(code.value)
})
var display =document.getElementsByClassName("textbox")[0];
function checkpassword(password)
{
var strength=0;
if (password.match(/[a-z]+/)){
strength+=1;
}
if (password.match(/[A-Z]+/)){
strength+=1;
}
if (password.match(/[0-9]+/)){
strength+=1;
}
if (password.match(/[$@#&!]+/)){
strength+=1;
}
if (password.length<6){
display.innerHTML="minimum number of characters is 6":
}
if (password.length>12){
display.innerHTML="maximum number of characters is 12";
}
switch(strength){
case 0:
strengthbar.value=0;
break;
case 1:
strengthbar.value=25;
break;
case 2:
strengthbar.value=50;
break;
case 3:
strengthbar.value=75;
break;
case 4:
strengthbar.value=100;
break; }
}
Above is my own solution to the problem. It isn't giving me any errors but the JavaScript is not generating any results on my html.
Here is a link to the pen I'm using
Share Improve this question edited May 27, 2018 at 1:10 Emmanuel Dan asked May 26, 2018 at 22:32 Emmanuel DanEmmanuel Dan 1611 gold badge1 silver badge6 bronze badges 16 | Show 11 more comments1 Answer
Reset to default 18You had several mistakes in your code (had because you fixed some by reading the comments). I'll list them again anyways:
- Your
switch
statement was missing a curly bracket ({
) at its beginning - Your function was missing a curly bracket (
{
) at its end - It's
getElementsByClassName
, notgetElementByClassName
. In addition to that,getElementsByClassName
returns an array like HTMLCollection, so you need to select the first one like this:getElementsByClassName('textbox')[0]
(mind the "[0]" here) - You don't call
innerHTML
likeinnerHTML("bla");
, it'sinnerHTML = "bla";
You also should always use semicolons (;
).
var code = document.getElementById("password");
var strengthbar = document.getElementById("meter");
var display = document.getElementsByClassName("textbox")[0];
code.addEventListener("keyup", function() {
checkpassword(code.value);
});
function checkpassword(password) {
var strength = 0;
if (password.match(/[a-z]+/)) {
strength += 1;
}
if (password.match(/[A-Z]+/)) {
strength += 1;
}
if (password.match(/[0-9]+/)) {
strength += 1;
}
if (password.match(/[$@#&!]+/)) {
strength += 1;
}
if (password.length < 6) {
display.innerHTML = "minimum number of characters is 6";
}
if (password.length > 12) {
display.innerHTML = "maximum number of characters is 12";
}
switch (strength) {
case 0:
strengthbar.value = 0;
break;
case 1:
strengthbar.value = 25;
break;
case 2:
strengthbar.value = 50;
break;
case 3:
strengthbar.value = 75;
break;
case 4:
strengthbar.value = 100;
break;
}
}
<body>
<form class="center-block">
<input type="text" id="password" autocomplete="off" class="form-control input-lg">
<progress max="100" value="0" id="meter"></progress>
<button class="btn btn-success btn-lg btn-block">Show Password Strength</button>
</form>
<div class="textbox text-center"> password </div>
</body>
document.getElementByClassName("textbox");
is not a function. It'sgetElementsByClassName
and should return an array like object. You also are not definingstrengthbar
anywhere in the code presented. – scrappedcola Commented May 26, 2018 at 22:45