const displayInput = document.getElementById(‘display_name’);
const usernameInput = document.getElementById(‘username’);
displayInput.addEventListener(‘keyup’, function () {
const displayName = this.value.toLowerCase();
// Sanitize display name for username
const sanitizedUsername = displayName.replace(/[^a-z0-9_]+/g, ‘-‘);
// Generate random numbers
const randomNumber = Math.floor(Math.random() * 9000) + 1000;
// Create username with random numbers
const finalUsername = sanitizedUsername + randomNumber;
// Ensure username meets WordPress requirements
if (finalUsername.length < 4) {
usernameInput.setCustomValidity('Username must be at least 4 characters long.');
} else if (!/^[a-z0-9_-]+$/i.test(finalUsername)) {
usernameInput.setCustomValidity('Username may only contain letters, numbers, hyphens, and underscores.');
} else {
usernameInput.setCustomValidity('');
}
// Update username field
usernameInput.value = finalUsername;
});