Constraint API: setCustomValidity for password, and small fixes

This commit is contained in:
NetMan 2024-02-24 17:51:52 +01:00
parent 84cb547df8
commit ec621d42b3
1 changed files with 18 additions and 21 deletions

View File

@ -19,18 +19,20 @@ function checkPasswordMatch() {
passwordInput.value === "" passwordInput.value === ""
) { ) {
passwordMatch.style.backgroundColor = ""; passwordMatch.style.backgroundColor = "";
passwordConfirmInput.setCustomValidity("Passwords don't match");
return false; return false;
} }
passwordMatch.style.backgroundColor = "#4b4"; passwordMatch.style.backgroundColor = "#4b4";
passwordConfirmInput.setCustomValidity("");
return true; return true;
} }
function checkPasswordRequirement(e, regex, element) { function checkPasswordRequirement(regex, requirementElement) {
if (e.value.search(regex) >= 0) { if (passwordInput.value.search(regex) >= 0) {
element.style.backgroundColor = "#4b4"; requirementElement.style.backgroundColor = "#4b4";
return true; return true;
} }
element.style.backgroundColor = ""; requirementElement.style.backgroundColor = "";
return false; return false;
} }
@ -41,23 +43,18 @@ function checkPasswordValidity() {
} else { } else {
passwordLength.style.backgroundColor = ""; passwordLength.style.backgroundColor = "";
} }
const lowercase = checkPasswordRequirement( const lowercase = checkPasswordRequirement(/[a-z]/, passwordLowercase);
passwordInput, const uppercase = checkPasswordRequirement(/[A-Z]/, passwordUppercase);
/[a-z]/, const number = checkPasswordRequirement(/\d/, passwordNumber);
passwordLowercase, const special = checkPasswordRequirement(/[#?!@$%^&*-]/, passwordSpecial);
); const suitsRequirements =
const uppercase = checkPasswordRequirement( length && lowercase && uppercase && number && special;
passwordInput, if (suitsRequirements) {
/[A-Z]/, passwordInput.setCustomValidity("");
passwordUppercase, } else {
); passwordInput.setCustomValidity("Password does not meet requirements");
const number = checkPasswordRequirement(passwordInput, /\d/, passwordNumber); }
const special = checkPasswordRequirement( return suitsRequirements;
passwordInput,
/[#?!@$%^&*-]/,
passwordSpecial,
);
return length && lowercase && uppercase && number && special;
} }
export { checkPasswordValidity, checkPasswordMatch }; export { checkPasswordValidity, checkPasswordMatch };