Webshop/public/registrieren/passwordValidation.js

46 lines
1.6 KiB
JavaScript

// Funktion, um die Stärke des Passworts zu bewerten
function checkPasswordStrength(password) {
const strengthBar = document.getElementById('passwordStrengthBar');
const passwordStrength = evaluatePasswordStrength(password);
// Aktualisiere den Sicherheitsbalken basierend auf der Stärke
if (passwordStrength === 'weak') {
strengthBar.classList.remove('medium', 'strong');
strengthBar.classList.add('weak');
} else if (passwordStrength === 'medium') {
strengthBar.classList.remove('weak', 'strong');
strengthBar.classList.add('medium');
} else {
strengthBar.classList.remove('weak', 'medium');
strengthBar.classList.add('strong');
}
}
// Funktion zur Beurteilung der Passwortstärke
function evaluatePasswordStrength(password) {
if (password.length >= 8 && /[A-Z]/.test(password) && /[0-9]/.test(password)) {
return 'strong';
} else if (password.length >= 6) {
return 'medium';
} else {
return 'weak';
}
}
// Event Listener für das Passwortfeld
document.getElementById('regPassword').addEventListener('input', function() {
checkPasswordStrength(this.value);
});
// Event Listener für das Bestätigungs-Passwortfeld
document.getElementById('confirmPassword').addEventListener('input', function() {
const password = document.getElementById('regPassword').value;
const confirmPassword = this.value;
if (password !== confirmPassword) {
this.setCustomValidity("Die Passwörter stimmen nicht überein.");
} else {
this.setCustomValidity("");
}
});