component StrongPassword { props { size = "default" color = "primary" label = "Password" name = "password" value = "" placeholder = "Create a strong password" autocomplete = "new-password" minLength = 8 specialCharactersSet = "!@#$%^&*()_+-=[]{}|;:,.<>?" requireLowercase = true requireUppercase = true requireNumber = true requireSpecialCharacter = true showRequirements = true presentation = "inline" hintText = "Use a unique password you do not use elsewhere." emptyLabel = "Enter a password" weakLabel = "Weak" fairLabel = "Fair" goodLabel = "Good" strongLabel = "Strong" disabled = false readonly = false required = false invalid = false validationMessage = "" class = "" @event input = function @event change = function @event strength = function } state password = value state detailsOpen = false functions { function hasMinimumLength() { return password.length >= Number(minLength) } function hasLowercase() { return password !== password.toUpperCase() } function hasUppercase() { return password !== password.toLowerCase() } function hasNumber() { return containsCharacterAt("0123456789", 0) } function hasSpecialCharacter() { return containsCharacterAt(specialCharactersSet, 0) } function containsCharacterAt(characters, index) { if (index >= characters.length) { return false } if (password.includes(characters[index])) { return true } return containsCharacterAt(characters, index + 1) } function score() { return (hasMinimumLength() ? 1 : 0) + (requireLowercase && hasLowercase() ? 1 : 0) + (requireUppercase && hasUppercase() ? 1 : 0) + (requireNumber && hasNumber() ? 1 : 0) + (requireSpecialCharacter && hasSpecialCharacter() ? 1 : 0) } function maximumScore() { return 1 + (requireLowercase ? 1 : 0) + (requireUppercase ? 1 : 0) + (requireNumber ? 1 : 0) + (requireSpecialCharacter ? 1 : 0) } function strengthPercent() { return Math.round(score() / maximumScore() * 100) } function strengthLevel() { if (!password) { return "empty" } if (strengthPercent() <= 25) { return "weak" } if (strengthPercent() <= 50) { return "fair" } if (strengthPercent() < 100) { return "good" } return "strong" } function strengthLabel() { if (strengthLevel() === "weak") { return weakLabel } if (strengthLevel() === "fair") { return fairLabel } if (strengthLevel() === "good") { return goodLabel } if (strengthLevel() === "strong") { return strongLabel } return emptyLabel } } view {
} }