Problem comparing boleano value from javascript

Asked

Viewed 54 times

0

I have a function validaSenha that I’m using that checks the amount of characters and returns true or false. In the action of the end button I execute a call in the function inside a if, but even validating does not issue the alert.

Debugging, I put one alert(validaSenha($("#senha").val())), and is returning correctly.

Someone could help me in this case?

$("#btn-finalizar").on('click', function(e) {
  e.preventDefault();

  if ( validaSenha($("#senha").val()) )
    alert('ok')

})

function validaSenha(senha) {
  var numeros = 0;
  var letras = 0;
  var caracteres = senha.split('');
  for (let car of caracteres) {
    if (car.match(/\d/)) numeros++; // se for numero
    else if (car.match(/\w/)) letras++; // se não for numero, pode ser letra
  }
  return (numeros+letras) >= 8 && (numeros + letras) == senha.length;
}
  • 1

    I tested it here and it worked, are you sure it’s not a typo? You have how to put the html used?

  • 1

    @Denis Rudnei de Souza I think the problem was with characters that are neither letters nor numbers.

1 answer

2


Hello, the problem is that you are not counting what is different from number and letter, as for example special character

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#btn-finalizar").on('click', function(e) {
  e.preventDefault();

  if ( validaSenha($("#senha").val()) )
    alert('ok');
    else
    alert("não é válida");

})

function validaSenha(senha) {
  var numeros = 0;
  var letras = 0;
  var caracterSepcial = 0;
  var caracteres = senha.split('');
  for (let car of caracteres) {
    if (car.match(/\d/)) numeros++; // se for numero
    else if (car.match(/\w/)) letras++; // se não for numero, pode ser letra
	else caracterSepcial++; // se não for número nem letra
  }
  return (numeros+letras +caracterSepcial) >= 8 && (numeros + letras + caracterSepcial) == senha.length;
}

  
});
</script>
</head>
<body>
<input type="button" id="btn-finalizar" value="Validar" />

<input type="text" id="senha" />
</body>
</html>

  • Very good. I would, still, deny the result twice, to force the conversion to boolean, error-independent within the function (eg: if (!!validaSenha($("#senha").val())))

  • 1

    @Arthur Siqueira there IF any error occurs in the function would always return false, that’s it?

  • Kind of. Javascript uses dynamic typing, which means that if by some random destination, you decide to modify the function to return other values (such as a numeric code for the password security level, and use the zero number as "invalid")the function will continue to work because !0 === true, and !!0 === false

  • 1

    If values like 0, undefined, '' (empty string) or null were denied twice, they saw false, and if any other value is denied twice (including objects), it becomes true

  • This way you ensure that the code follows a standardization, and works independently of later changes

  • 1

    @Arthur Siqueira That’s what I figured.

  • 1

    This is also an interesting way to test if a string is empty, if the user has typed null values (0, empty field, etc.) or even if the function is returning the object you need. Another advantage is that because it is a conversion, you can use it almost anywhere, when you need to avoid errors

  • 1

    @Arthur Siqueira Very interesting, I will test the next few times, thank you very much.

  • 1

    Mto thanks Arthur!

Show 4 more comments

Browser other questions tagged

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