Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

204
Views
Why is one of my functions isn´t working while onclick event?

I have a form with fields for password and confirm password and other input fields. I created a function that calls other functions. Basically this one function calls a function that shows a confirmation message before submitting my form, and the other function is a validation for the two password fields to be equal.

So the function that is showing the confirmation message does work but the validation for passwords doesnt.

function confirmarAlta(){
            
            var result = confirm("Esta seguro que desea crear este usuario?");
            
            if(result == false){
                event.preventDefault();
            }
        }
    
    
 function verificarPassword(){
        var pass = document.querySelector(".password").value;
        var confirmPass = confirmPassword.querySelector(".confirmPassword").value;
        console.log(pass);
        console.log(confirmPass)
            
        if(pass != confirmPass){
            alert("Las contraseñas no coinciden");
            pass = "";
            confirmPass = "";
        }
        
    }   
    
    
function Validaciones(){
            verificarPassword();
            confirmarAlta();
        }
<div class="form-group">
              <label>Contraseña: </label>
              <input type="password" class="form-control password" name="txtPassword" required>
            </div>
            <div class="form-group">
              <label>Repetir Contraseña: </label>
              <input type="password" class="form-control confirmPassword"  name="txtConfirmarPassword" required>
            </div>
          
          
          
          <input onclick="Validaciones()" type="submit" class="btn btn-success" value="Aceptar" name="btnAceptar">

The error im getting is confirmPassword is not defined.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

When you are selecting the confirm password input, you are selecting from confirmPassword instead of document.

Do this change:

// Current
confirmPassword.querySelector(".confirmPassword").value

// Change by
document.querySelector(".confirmPassword").value
about 4 years ago · Juan Pablo Isaza Report

0

function confirmarAlta(){
            
            var result = confirm("Esta seguro que desea crear este usuario?");
            
            if(result == false){
                event.preventDefault();
            }
        }
    
    
 function verificarPassword(){
        var pass = document.querySelector(".password").value;
        var confirmPass = document.querySelector(".confirmPassword").value;
        console.log(pass);
        console.log(confirmPass)
            
        if(pass != confirmPass){
            alert("Las contraseñas no coinciden");
            pass = "";
            confirmPass = "";
        }
        
    }   
    
    
function Validaciones(){
            verificarPassword();
            confirmarAlta();
        }
<div class="form-group">
              <label>Contraseña: </label>
              <input type="password" class="form-control password" name="txtPassword" required>
            </div>
            <div class="form-group">
              <label>Repetir Contraseña: </label>
              <input type="password" class="form-control confirmPassword"  name="txtConfirmarPassword" required>
            </div>
          
          
          
          <input onclick="Validaciones()" type="submit" class="btn btn-success" value="Aceptar" name="btnAceptar">

about 4 years ago · Juan Pablo Isaza Report

0

Looks like a missing semi-colon at the end of the line...

console.log(confirmPass)

Also, is the intention for the password confirmation failure to clear the input boxes for the user? Or just those local variables? You could get the object reference instead of the value in those variables, and then reset the value if that's what you want.

Nothing else is jumping out at me, but yes... definitely learn to use the dev tools built into the browser (usually you can get there by pressing the 'F12' key). You can set break points in the javascript and walk through the code execution to see where the errors are happening.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!