0
I have two inputs within my form: Password and Confirm Password
Before the user submit the form, I would like to compare the value of the two inputs. If they are equal, send the form normally. If there is divergence between them, it would display a "textile" just below the input of Confirm Password.
I tried to do here, but the form is always sent, even if the passwords are different. Below is what I have done:
function validarSenha() {
  senha = document.getElementsByName('senha').value;
  senhaC = document.getElementsByName('senhaC').value;
  if (senha != senhaC) {
    senhaC.setCustomValidity("Senhas diferentes!");
    return false;
  } else {
    return true;
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
  <input required="" type="password" name="senha" id="senha" placeholder="Senha">
  <input required="" type="password" name="senhaC" id="senhaC" placeholder="Confirmar Senha">
  <br><br>
  <button type="submit" onclick="return validarSenha()">Enviar</button>
</form>
getElementsByNamedoes not return an array? Voce should not placesenha = document.getElementsByName('senha')[0].value;? because I think your code fromifare checking outundefined != undefined, soon it will always befalse.– Cmte Cardeal
I tried that way, but it turned out the same...
– Afuro Terumi