How to validate password through JavaScript

0

I have a form that I need to confirm if the fields, NovaSenha and CNovaSenha, are the same. Only if the two fields are the same as the system will allow from the post to the file_exchange.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>
    
asked by anonymous 25.08.2014 / 19:38

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 encountered was that you were storing another value in the same variable, and testing another variable

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

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

    
25.08.2014 / 19:43
2

Add in Form o onsubmit : onsubmit="return validarSenha();"

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

Change type from button to Submit :

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

And then in the 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;
 }
    
25.08.2014 / 19:44