2
I can’t prevent the button submit to carry out its action if the information typed is not equal, someone could help me?
HTML
<form method="post" action="php/cadastro.php">
    <span class="form_title">Cadastre-se</span>
    <fieldset>
        <input type="text" name="nome" class="txt_input first_input" placeholder="Nome">
        <input type="email" name="email" class="txt_input" id="email" placeholder="E-mail">
        <input type="email" name="confirmaEmail" class="txt_input" id="confirma-email" placeholder="Confirmar e-mail">
        <input type="password" name="senha" class="txt_input" id="senha" placeholder="Senha">
        <input type="password" name="confirmaSenha" class="txt_input" id="confirma-senha" placeholder="Confirmar senha">
    </fieldset>
    <fieldset>
        <input type="checkbox" name="termos">
        <p>Eu concordo com os <span>termos de uso e política de privacidade</span> do WEB TRAINING</p>
    </fieldset>
    <fieldset class="buttons">
        <input type="submit" name="Cadastrar" value="Cadastrar" class="log_btn" id="cadastro-btn" onclick="valida()">
        <input type="button" name="Voltar" value="Voltar" class="log_btn volta_btn" id="volta-btn" onclick="voltar()">
    </fieldset>
</form>
Javascript
   var email = document.getElementById('email');
var confirmaEmail = document.getElementById('confirma-email');
var senha = document.getElementById('senha');
var confirmaSenha = document.getElementById('confirma-senha');
var cadastrar = document.getElementById('cadastro-btn');
    function valida(){
        if(email != confirmaEmail || senha != confirmaSenha){
            cadastrar.preventDefault();
        }
    };
*The script is already linked at the end of the HTML *I tried the ways you sent, but none worked
Missing operator (OR) in condition.
if(email != confirmaEmail || senha != confirmaSenha){ ...and to get the value of the fields do not forget the.valueex:var confirmaSenha = document.getElementById('confirma-senha').value;– rray
Note also that you own a
)extra at the end of the functionvalida. Unless it was a typo when migrating the question.– celsomtrindade
still doesn’t work, code fixed
– Murilo Melo
I put it corrected below
– Miguel