odin-sign-up-form/script.js

36 lines
898 B
JavaScript

const passwordFields = document.querySelectorAll('input[type="password"');
function checkPassword() {
const passwordMatch = Array.from(passwordFields).every(element => {
if (element.value == passwordFields[0].value) {
return true;
}
});
return passwordMatch;
};
function showPasswordMatch() {
passwordFields.forEach(element => {
element.nextSibling.remove();
});
if (checkPassword()) {
passwordFields.forEach(element => {
element.classList.remove("psk-no-match");
})
} else {
passwordFields.forEach(element => {
element.classList.add("psk-no-match");
const info = document.createElement("span");
info.textContent = "* Passwords do not match";
info.classList.add("psk-no-match");
element.after(info);
})
}
}
passwordFields.forEach(element => {
element.addEventListener('keyup', showPasswordMatch);
});