I would leave to use Javascript when it really was needed. Animations and changing the appearance of an element can only be done with CSS.
You can use selectors per attribute to apply rules according to the set value attributes of an element. Thus, you can use any global attribute to define text to identify the force of the password ( fraca
, normal
, forte
, etc).
Styling (including animation) would be done with CSS, based on text that identifies the password strength. How about using the title
attribute for this?
/* Quando o valor do atributo 'title' for 'forte'... */
input[title='forte'] {
/* ... fique verde. */
}
/* Quando o valor do atributo 'title' for 'fraca'... */
input[title='fraca'] {
/* ... fique vermelho. */
}
Looks cool, huh?!
Now, there is a need to use Javascript (or JQuery), and its use is only to count the number of characters entered and to change the title
attribute value. This is because it is not yet possible to make selection by amount of characters, otherwise neither Javascript would be necessary.
In the examples below, if the password length is:
-
<= 2
: The background will be red, taking up 33% of the width of the element.
-
> 2 && < 8
: The background will be yellow, occupying 66% of the width of the element.
-
>= 8
: The background will be green, occupying 100% width of the element.
jQuery:
$(function() {
$('input').on('keyup', function() {
// Obtém a quantidade de caracteres do valor inserido no input.
var length = $(this).val().length;
// Por padrão, o texto será 'Força da senha', caso a quantidade
// de caracteres seja menor que 1.
var title = 'Força da senha';
if (length > 0) {
if (length <= 2)
title = 'fraca';
else if (length > 2 && length < 8)
title = 'normal';
else
title = 'forte';
}
// Altera o atributo título com a palavra que identifica força da senha.
$('.password-strength').attr('title', title);
});
});
.password-strength {
display: block;
height: 12px;
position: relative
}
.password-strength::after {
content: attr(title);
color: #fff;
font-size: 10px;
position: absolute;
top: 0;
bottom: 0;
left: 0;
transition: width 200ms ease-in-out;
text-align: center
}
.password-strength[title='Força da senha']::after {
background-color: #333;
width: 100%
}
.password-strength[title='fraca']::after {
background-color: #e74c3c;
width: 33%
}
.password-strength[title='normal']::after {
background-color: #f1c40f;
width: 66%
}
.password-strength[title='forte']::after {
background-color: #2ecc71;
width: 100%
}
input, span { margin: 4px }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputtype='password'placeholder='Senha'/><spantitle='Forçadasenha'class='password-strength'></span>
VanillaJS:
document.querySelector('input').addEventListener('keyup', function() {
// Obtém o total de caracteres do valor inserido.
var length = this.value.length;
var title = 'Força da senha';
if (length > 0) {
if (length <= 2)
title = 'fraca';
else if (length > 2 && length < 8)
title = 'normal';
else
title = 'forte';
}
// Altera o atributo 'title' com a palavra que identifica a força da senha.
document.querySelector('.password-strength').setAttribute('title', title);
}, false);
.password-strength {
display: block;
position: relative;
height: 12px
}
.password-strength::after {
content: attr(title);
color: #fff;
font-size: 10px;
position: absolute;
top: 0;
bottom: 0;
left: 0;
transition: width 200ms ease-in-out;
text-align: center
}
.password-strength[title='Força da senha']::after {
background-color: #333;
width: 100%
}
.password-strength[title='fraca']::after {
background-color: #e74c3c;
width: 33%
}
.password-strength[title='normal']::after {
background-color: #f1c40f;
width: 66%
}
.password-strength[title='forte']::after {
background-color: #2ecc71;
width: 100%
}
input { margin: 4px }
<input type='password' placeholder='Senha' />
<span title='Força da senha' class='password-strength'></span>