alculates the strength of the given password, suitable for use in updating * a progress bar that represents said strength. * * The strength of the password is calculated by checking the number of: * - Characters * - Numbers * - Non-alphanumeric chars * - Upper case characters * * @param {string} password * The password to calculate the strength of. * @returns {number} * The strength of the password in the range [0, 100]. */ function getPasswordStrength(password) { let lengthStrength = password.length; if (lengthStrength > 5) { lengthStrength = 5; } let nonNumericChars = password.replace(/[0-9]/g, ""); let numericStrength = password.length - nonNumericChars.length; if (numericStrength > 3) { numericStrength = 3; } let nonSymbolChars = password.replace(/\W/g, ""); let symbolStrength = password.length - nonSymbolChars.length; if (symbolStrength > 3) { symbolStrength = 3; } let nonUpperAlphaChars = password.replace(/[A-Z]/g, ""); let upperAlphaStrength = password.length - nonUpperAlphaChars.length; if (upperAlphaStrength > 3) { upperAlphaStrength = 3; } let strength = lengthStrength * 10 - 20 + numericStrength * 10 + symbolStrength * 15 + upperAlphaStrength * 10; if (strength < 0) { strength = 0; } if (strength > 100) { strength = 100; } return strength; } /** * oninput() handler for both password textboxes. * * @param {boolean} recalculatePasswordStrength * Whether to recalculate the strength of the first password. */ function onPasswordInput(recalculatePasswordStrength) { let pw1 = document.getElementById("pw1").value; if (recalculatePasswordStrength) { document.getElementById("pwmeter").value = getPasswordStrength(pw1); } // Disable the accept button if the two passwords don't match, and enable it // if the passwords do match. let pw2 = document.getElementById("pw2").value; document.getElementById("setp12password").getButton("accept").disabled = pw1 != pw2; } window.addEventListener("load", () => onLoad()); PK