How to validate password through Javascript

Asked

Viewed 17,094 times

0

I have a form that I need to confirm that the fields, Novapassword and Cnovapassword, are equal. Only if the two fields are equal will the system allow from the post to the file troca_password.php If it can be with jquery, it’s even better. I accept suggestions.

<html>
    <head>
    <title>Reset de Senha</title>
    <script>
                function validarSenha()
                    NovaSenha = document.FormSenha.NovaSenha.value;
                    NovaSenha = document.FormSenha.CNovaSenha.value;

                    if (NovaSenha != CNovaSenha) 
                        alert("SENHAS DIFERENTES!\\nFAVOR DIGITAR SENHAS IGUAIS");
            </script>
            </head>
        <body>

    <div class="container-fluid container_reset">
                <div class="row-fluid">
                    <div class="well">
                        <form action="troca_senha.php" method="POST" id="FormSenha" name="FormSenha">
                            <div class="centraliza_reset">
                                <fieldset>
                                    <br /><br />
                                    <div class="row">
                                    <div class="span2"></div>
                                    <div class="span3">Nova Senha:</div>
                                    <div class="span6"><input type="password" maxlength="10" name="NovaSenha" /></div>
                                    </div>
                                    <div class="row">
                                    <div class="span2"></div>
                                    <div class="span3">Confirme a nova Senha:</div>
                                    <div class="span6"><input type="password" maxlength="10" name="CNovaSenha" /></div>
                                    </div>

                                    <button type="reset" class="btn btn-primary pull-left botao_limpar_senha">Limpar</button>
                                    <button type="button" class="btn btn-primary pull-right botao_reset_senha" onClick="validarSenha()">Enviar</button>
                                </fieldset>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
    </body>
</html>

2 answers

3


Your code is correct!

<html>
    <head>
    <title>Reset de Senha</title>
    <script>
function validarSenha(){
   NovaSenha = document.getElementById('NovaSenha').value;
   CNovaSenha = document.getElementById('CNovaSenha').value;
   if (NovaSenha != CNovaSenha) {
      alert("SENHAS DIFERENTES!\nFAVOR DIGITAR SENHAS IGUAIS"); 
   }else{
      document.FormSenha.submit();
   }
}
        </script>
        </head>
    <body>

<div class="container-fluid container_reset">
            <div class="row-fluid">
                <div class="well">
                    <form action="troca_senha.php" method="POST" id="FormSenha" name="FormSenha">
                        <div class="centraliza_reset">
                            <fieldset>
                                <br /><br />
                                <div class="row">
                                <div class="span2"></div>
                                <div class="span3">Nova Senha:</div>
                                <div class="span6"><input type="password" maxlength="10" id="NovaSenha" /></div>
                                </div>
                                <div class="row">
                                <div class="span2"></div>
                                <div class="span3">Confirme a nova Senha:</div>
                                <div class="span6"><input type="password" maxlength="10" id="CNovaSenha" /></div>
                                </div>

                                <button type="reset" class="btn btn-primary pull-left botao_limpar_senha">Limpar</button>
                                <button type="button" class="btn btn-primary pull-right botao_reset_senha" onClick="validarSenha()">Enviar</button>
                            </fieldset>
                        </div>
                    </form>
                </div>
            </div>
        </div>
</body>

The only error I found was that you were storing another value in the same variable, and testing with another variable

----- EDITED------

I recommend using id instead of name, it’s easier to recover the value np backend...

  • I tested both examples and it’s not working. I typed 123 and 456 in the second field and gave Submit anyway. It would be because of a code position?

  • 1

    ready, I’ve edited the code

  • Thank you. .

2

Add to Form the onsubmit: onsubmit="return validarSenha();"

<form action="troca_senha.php" method="POST" id="FormSenha" name="FormSenha" onsubmit="return validarSenha();">

Modify the type of button for Submit:

<button type="submit" class="btn btn-primary pull-right botao_reset_senha">Enviar</button>

And then in function JavaScript:

 function validarSenha(){
        NovaSenha = document.FormSenha.NovaSenha.value;
        CNovaSenha = document.FormSenha.CNovaSenha.value;
        if (NovaSenha != CNovaSenha){ 
             alert("SENHAS DIFERENTES!\\nFAVOR DIGITAR SENHAS IGUAIS");
             return false;
        }
        return true;
 }
  • 1

    @bfavaretto, true, no need. I removed.

Browser other questions tagged

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