odin-sign-up-form/script.js

36 lines
898 B
JavaScript
Raw Normal View History

2024-01-25 21:17:05 +00:00
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);
});