Last active
March 23, 2025 18:54
-
-
Save jaoltr/24ea048dd757e9aaa409c9f4a4d12fe6 to your computer and use it in GitHub Desktop.
Fix autofill name duplication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.addEventListener("DOMContentLoaded", function() { | |
const nameFirstInput = document.querySelector('input[name="name_first"]'); | |
const nameLastInput = document.querySelector('input[name="name_last"]'); | |
// Function to convert text to proper case | |
function toProperCase(text) { | |
return text | |
.toLowerCase() | |
.replace(/\b\w/g, function(char) { return char.toUpperCase(); }); | |
} | |
// Function to update the name fields based on the specified conditions | |
function updateNameFields() { | |
let nameFirst = nameFirstInput.value.trim(); | |
let nameLast = nameLastInput.value.trim(); | |
// If name_first == name_last, check for spaces and split accordingly | |
if (nameFirst === nameLast && nameFirst.includes(" ")) { | |
const nameParts = nameFirst.split(" "); | |
nameFirstInput.value = nameParts.slice(0, -1).join(" "); // All but the last part | |
nameLastInput.value = nameParts[nameParts.length - 1]; // The last part | |
} | |
// Convert both name_first and name_last to proper case if they are all uppercase | |
if (nameFirst === nameFirst.toUpperCase()) { | |
nameFirstInput.value = toProperCase(nameFirst); | |
} | |
if (nameLast === nameLast.toUpperCase()) { | |
nameLastInput.value = toProperCase(nameLast); | |
} | |
// Handle case where name_first is in name_last (remove from name_last) | |
const nameLastParts = nameLast.split(" "); | |
const nameFirstWords = nameFirst.split(" "); | |
nameLastInput.value = nameLastParts.filter(word => !nameFirstWords.includes(word)).join(" "); | |
// Handle case where name_last is in name_first (remove from name_first) | |
const nameFirstParts = nameFirst.split(" "); | |
nameFirstInput.value = nameFirstParts.filter(word => !nameLastParts.includes(word)).join(" "); | |
} | |
// Listen for changes on both inputs | |
nameFirstInput.addEventListener('input', updateNameFields); | |
nameLastInput.addEventListener('input', updateNameFields); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment