0

I'm trying to check whether a user has added at least 8 characters in a password field and add some validation text if they've not but I'm getting this in my console: script.js:13 Uncaught TypeError: Cannot read properties of null (reading 'addEventListener') at script.js:13:17

I believe it relates to this line:

confirmPassword.addEventListener('change', checkPassword);

Full code:

let password = document.querySelector('#password');
let confirmPassword = document.querySelector('#confirm_password');
let passwordValidation = document.createElement('p');
let passwordValidationText = ''

passwordChecker => {
if (password.length <8) {
passwordValidationText === 'Please enter a password, longer than 8 characters';
password.appendChild(passwordValidation);
}
};

confirmPassword.addEventListener('change', passwordChecker);

And the input fields:

<label for="password">Password</label>
        <input type="password" id="password" name="password">
        </div>
        <div class="item">
        <label for="confirm_password">Confirm password</label>
        <input type="password" id="confirm_password" name="confirm_password">

My thinking being I only want to validate when the user has finished typing.

Any idea what I'm doing wrong?

Sam Jefferies
  • 584
  • 1
  • 7
  • 27

0 Answers0