1

I want it to check if the user entered the correct format for the password (which is at least one lowercase, one uppercase, one number, eight or more characters, and one special character) but I couldn't get it to validate.

JS:

var Pass = document.getElementById('password');
function validatePassword() {
  if (Pass.value.match(/^(?=.*[0-9])(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{8,}$/)) {
    document.getElementById('pass').innerHTML =
      'Invalid password. Please follow the format.';
    return false;
  }
}

rjgrl
  • 61
  • 4
  • 1
    Do you call `validatePassword()` anywhere? Please update the question to include a [mcve] demonstrating the problem. This is also a good time to use your browser's debugging tools to step through the code as it executes and observe the behavior, narrowing down the problem to a specific operation and specific values used. – David Dec 22 '21 at 19:08
  • https://stackoverflow.com/questions/19605150/regex-for-password-must-contain-at-least-eight-characters-at-least-one-number-a – ubaidh khatlani Dec 22 '21 at 19:11

1 Answers1

-1

adding more expression will help. for checking the cap and small letters. Also for checking the password, you need some sort of button to use it as a trigger to call the function.

or u can use "oninput" event listener.

var Pass = document.getElementById('password');
// lets say your validator button hase id of validate;
let validator=document.getElementById("validate");
validator.onclick=validatePassword(Pass.value);
// or use on input method like
Pass.oninput=validatePassword(Pass.value);
function validatePassword(value) {
let reg=/^(?=.*[0-9])(?=.*[!@#$%^&*])(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9!@#$%^&*]{8,}$/;
  if (!reg.test(value) {
    Pass.innerHTML =
      'Invalid password. Please follow the format.';
    return false;
  }
return true;
}