Skip to content

Commit

Permalink
Refactor password validation and matching logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tigheni committed Mar 1, 2024
1 parent 1bf7d16 commit 5f0c86a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
51 changes: 47 additions & 4 deletions MAIN.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
const SubmitBtn = document.getElementById("submit-btn");
const password = document.querySelector("#password");
const repassed = document.querySelector("#password-con");
const Message = document.getElementById("Message");
/*
async function check() {
let reg = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[a-zA-Z]).{8,}$/;
Expand Down Expand Up @@ -39,3 +36,49 @@ SubmitBtn.addEventListener("click", (e) => {
e.preventDefault();
}
});
*/

const submitBtn = document.getElementById("submit-btn");
const passwordInput = document.querySelector("#password");
const retypePasswordInput = document.querySelector("#password-con");
const messageElement = document.getElementById("Message");

async function isValidPassword(password) {
const reg = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[a-zA-Z]).{8,}$/;
return reg.test(password);
}

function confirmPasswordMatch() {
const password = passwordInput.value;
const retypePassword = retypePasswordInput.value;

if (password === "" && retypePassword === "") {
showMessage("Password should be matched", "blue");
return false;
} else if (password === retypePassword) {
showMessage("Passwords match!", "green");
return true;
} else {
showMessage("Passwords do NOT match!", "red");
return false;
}
}

function showMessage(message, color) {
messageElement.textContent = message;
messageElement.style.color = color;
}

function checkPasswordMatch() {
passwordInput.addEventListener("input", confirmPasswordMatch);
retypePasswordInput.addEventListener("input", confirmPasswordMatch);
}
checkPasswordMatch();

submitBtn.addEventListener("click", async (e) => {
const isPasswordValid = await isValidPassword(passwordInput.value);
const doPasswordsMatch = confirmPasswordMatch();
if (!isPasswordValid || !doPasswordsMatch) {
e.preventDefault();
}
});
2 changes: 1 addition & 1 deletion style.css
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ input {
border: 1px solid #e5e7eb;
border-radius: 10px;
}
input:valid {
input:valid {
outline: none;
border: 1px solid #e5e7eb;
}
Expand Down

0 comments on commit 5f0c86a

Please sign in to comment.