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

214
Views
Validation of the password field and the confirm password field

Good night! I'm developing a project for study and I really wanted to do the validation of the password field and the validation of the confirmSenha field. The idea is that when the user enters a different password, a message is returned saying that the passwords do not match. Below is the scope of my code.

function validarPasswordConfirm(password, confirmPassword) {

  var typedpassword = document.getElementsByName('password').value;
  var confirmTypedPassword = document.getElementsByName('passwordConfirm').value;

  if(typedpassword !== confirmTypedPassword) {
    return { value: false, text: 'Passwords are not the same.' }
  } else {
    return { value: true, text: '' }
  }
}

I'm trying to validate the password field with the confirmpassword field

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

0

The function getElementsByName returns an array, so you can't access the value property like that.

I recommend use getElementById instead.

about 4 years ago · Juan Pablo Isaza Report

0

I noticed a few problems with your code.


Firstly, var is not used anymore (see why). You should use an alternative.

Alternatives for var are below.

  1. let - Use this for variables that can be changed.
  2. const - Use this for variables that cannot be changed (constant).

Secondly, getElementsByName() returns a NodeList, which is an iterable (like an array). This means that .value is undefined in a NodeList.

To fix it, change your code to below.

const typedPassword = document.querySelector("#password").value;
const confirmTypedPassword = document.querySelector("#passwordConfirm").value;
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!