How you use the keyup in a specific input

Asked

Viewed 320 times

0

I have made a javascript to verify passwords (forte/normal/fra ca) the problem is that using the keyup in javascript every time a input receives value the password level is changed, need to make the password level is only changed logically when the input password receive value.

Javascript

function validaSenha() {

 $('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 <= 8)
      title = 'fraca';

    else if (length >=8  && length < 10)
      title = 'normal';

    else 
      title = 'forte';
  }

  // Altera o atributo título com a palavra que identifica força da senha.
  $('.password-strength').attr('title', title);

 });

}

HTML

<script type="text/javascript" src="js/senhaForte.js"></script> 
   <form name="formCadastro" class="formCadastro" method="post">
     <div class="input-field col s12">
        <i class="mdi-action-https prefix"></i> 
        <input id="senha" type="password" required name="senha" minlength="8" maxlength=32 class="validate" placeholder='Senha'  onkeydown="validaSenha()"/>
        <label for="senha">Senha</label>
        <span title='' class='password-strength'></span>
     </div>
    </form>

2 answers

3


In the selector use the class or id:

$('input#senha').on('keyup', function() {

or just

$('#senha').on('keyup', function() { 

0

Use id to select specific input.

$( "#target" ).keyup(function() {
  alert( "Função .keyup() chamada." );
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<form>
  <input id="target" type="text" placeholder="Digite aqui">
</form>
<div id="other">
  Teste o KeyUp
</div>

#target is the desired input id

source: https://api.jquery.com/keyup/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.