"Submit" button, check the number of digits in the password and if it is the same as another password in html / javascript

-1

I'm making a form, where the "submit" button needs to check the number of digits and check if it matches another password.

html code

<div class="content">
   <input type="submit" class="botao01"  onclick="Salvar();" value="Salvar" />
</div>

Javascript

function Salvar(){
    var senha1 = document.f1.senha1.value;
    var senha2 = document.f1.senha2.value;

    if(senha1 === senha2){
        alert("SENHAS IGUAIS");
    }else{
        alert("SENHAS DIFERENTES");
    }
    if(senha.lenght>=6){
       alert("Dígitos minimo é 6");
    }
}

But you're not checking ...

    
asked by anonymous 09.09.2018 / 16:55

1 answer

0

Come on:

  • Avoid using onclick direct in HTML, this usage is very old and discouraged nowadays, create the direct listener in JS.

  • Do not access elements directly as you are trying ( document.f1.senha1.... ), use the standard JS search that is most guaranteed, either by id ( document.getElementById ) or even querySelector ( document.querySelector("...") )

  • To be required to enter at least 6 digits, use length < 6 (size less than 6) and do this check before others, so if you do not have the minimum characters , nor does it have to check if they are the same, since they are already invalid even ...

  • In the code below, you can check everything I mentioned above in a working example.

    document.getElementById("botaoSalvar").onclick = Salvar;
    
    function Salvar(){
      var senha1 = document.getElementById("senha1").value;
      var senha2 = document.getElementById("senha2").value;
    
      if(senha1.length < 6 || senha2.length < 6){
         console.log("Dígitos minimo é 6");
         return;
      }
    
      if(senha1 === senha2){
         console.log("SENHAS IGUAIS");
      }else{
          console.log("SENHAS DIFERENTES");
          return;
      } 
    }
    <div class="content">
        <div class="input-div" id="input-senha1">
          <b>Senha:</b> 
          <input type="password" required id="senha1" name="senha1" placeholder="Insira senha..." />
          <input type="password" required id="senha2" name="senha2" placeholder="Senha novamente..." />
        </div> 
        <input type="submit" class="botao01" id="botaoSalvar" value="Salvar" />
      </div>
        
    10.09.2018 / 22:32