<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Password Match Validation</title>
<style>
form#forgot-password {
display: flex;
flex-direction: column;
width: 300px;
border: 2px solid black;
border-radius: .5rem;
background-color: mediumturquoise;
font: 700 .95rem/1 system-ui, monospace, sans-serif;
}
form#forgot-password * {
display: block;
box-sizing: border-box;
}
form#forgot-password .title {
color: white;
background-color: cornflowerblue;
padding: .7rem;
margin-bottom: 1.5rem;
}
form#forgot-password input {
font-size: 1rem;
border: 2px solid black;
border-radius: .25rem;
margin: 0 auto;
width: 85%;
padding-left: .5rem;
}
form#forgot-password input + input {
margin-top: .75rem;
}
form#forgot-password input::placeholder {
font-size: 70%;
}
form#forgot-password .match-error {
color: orangered;
font-size: .9rem;
margin: .75rem auto;
}
form#forgot-password [type="submit"] {
border-radius: .25rem;
padding: .25rem 1rem;
cursor: pointer;
width: 85%;
margin: 0 auto .5rem;
}
form#forgot-password [type="submit"][disabled] {
cursor: default;
}
form#forgot-password .hide-match-error {
visibility: hidden;
}
</style>
</head>
<body>
<form id="forgot-password">
<div class="title">Forgot Password?</div>
<input type="password" id="password"
placeholder="Enter NEW Password">
<input type="password" id="password-confirm"
placeholder="Confirm NEW Password">
<div class="match-error hide-match-error">Passwords do not match!</div>
<button type="submit" disabled>Submit</button>
</form>
<script>
const forgot_password__form = document.querySelector('#forgot-password')
forgot_password__form.addEventListener('keydown', blockingProhibitedCharacters);
forgot_password__form.addEventListener('keyup', checkPasswordsMatching);
function blockingProhibitedCharacters(e) {
if (!e.target.matches('[type="password"]')) return;
if (e.code === 'Space') {
e.preventDefault();
e.target.dataset.ignoreKeyup = "true";
} else {
delete e.target.dataset.ignoreKeyup;
}
}
function checkPasswordsMatching(e) {
if (!e.target.matches('[type="password"]')) return;
if (e.target.dataset.ignoreKeyup === "true") return;
const password = e.currentTarget.querySelector('#password').value,
password_confirm = e.currentTarget.querySelector('#password-confirm').value,
match_error__message = e.currentTarget.querySelector('.match-error'),
submit__button = e.currentTarget.querySelector('[type="submit"]');
if (password === password_confirm && password.length > 0) {
match_error__message.classList.add('hide-match-error');
submit__button.disabled = false;
} else if (password.length === 0) {
match_error__message.classList.add('hide-match-error');
submit__button.disabled = true;
} else {
match_error__message.classList.remove('hide-match-error');
submit__button.disabled = true;
}
}
</script>
</body>
</html>